关于作者

姓名:傻馒头

性别:男

出生日期:1981-12-07

地区:中国-上海

联系电话:

QQ:--

婚否:未婚
用户名:shamantou
笔名:傻馒头
地区: 中国-上海
行业:其他

日历  

快速登录

+ 用户名:
+ 密 码:

在线留言



邻居们

资源角

朋友们

访问统计:
文章个数:294
评论个数:266
留言条数:59




Powered by BlogDriver 2.1 /div>

傻 馒 头 的 小 窝

 

有点骄傲,绝非自恋;有点可爱,绝非假装;
有点小智,绝非圣贤;有点傻帽,绝非笨蛋;
有点小气,绝非吝啬;有点馒头,绝非包子!

文章

Good Good Study, Day Day UP!  (作者置顶)

       

- 作者: 傻馒头 2005年11月9日, 星期三 20:17  回复(3) |  引用(1) 加入博采

最终的blog地址已定

- 作者: 傻馒头 2007年12月17日, 星期一 22:31  回复(0) |  引用(1) 加入博采

疯狂的投递简历
咋么没有想象中嘎么多的公司给我面试通知?

- 作者: 傻馒头 2007年11月28日, 星期三 10:54  回复(0) |  引用(1) 加入博采

[转] set

#include   <set>   
#include   <iostream>  
   
  class   A  
  {  
  public:  
          A(nt   i):   m(i){}  
          bool   operator   <   (const   A&   rhs)const  
          {return   m   <   rhs.m;}  
  public:  
          int   m,n;  
  };  
   
  int   main()  
  {  
          std::set<A>   s;  
          A   a(1);  
          A   b(1);  
          A   c(1);  
          s.insert(a);  
          s.insert(b);  
          s.insert(c);  
           
          std::set<A>::iterator   it   =   s.begin();  
          while(it   !=   s.end())  
          {  
                  std::cout   <<   (*it).m   <<   std::endl;  
                  it++;  
          }  
  }  
   
  打印结果我发现只有一个1,而不是三个1,也就是说,set容器中只加入了一个类A的对象实例

========================

stl::set 的一个缺陷

 

什么都好,就一点不好:不能仅仅通过 key去查找元素。

 

#include <set >

struct my_struct

{

    int key;

    int value1;

    int value2;

//……..….

//……

    explicit my_struct(int key_) // explicit 禁止将 int 悄悄转化为 my_struct

        : key( key_)

    {

        //....

    }

    struct compare

    {

        bool operator()(const my_struct& l, const my_struct& r) const

        {

            return l.key < r. key;

        }

        bool operator()(const int key, const my_struct& r) const

        {

            return key < r .key;

        }

        bool operator()(const my_struct& l, const int key) const

        {

            return l.key < key;

        }

    };

    struct ptr_compare

    {

        bool operator()(const my_struct* l, const my_struct* r) const

        {

            return l->key < r-> key;

        }

        bool operator()(const int key, const my_struct* r) const

        {

            return key < r ->key;

        }

        bool operator()(const my_struct* l, const int key) const

        {

            return l->key < key;

        }

    };

};

//….

//….

typedef std:: set<my_struct , my_struct:: compare> my_set_type ;

typedef std:: set<my_struct *, my_struct:: ptr_compare> my_ptr_set_type ;

//….

//….

void foo()

{

    my_set_type my_set;

    my_struct   x( 1);

    //....

    my_set. insert(x );

 

    my_struct   y( 100);

    my_set. insert(y );

    //.....

 

    // 错误,不能这样,如果把 explicit my_struct(int key_)

    // 中的 explicit 去掉,这样就可以,但仍然(悄悄地)

    // 增加了创建一个 my_struct 的开销

    my_set_type:: iterator iter2 = my_set. find(100 );

 

    // 只能这样

    my_struct asKey(100 );

    my_set_type:: iterator iter1 = my_set. find(asKey );

    // 或者这样,都增加了创建一个 my_struct 的开销

    my_set_type:: iterator iter3 = my_set. find(my_struct (100));

}

 

void foo2()

{

    my_ptr_set_type my_ptr_set;

    my_struct   x( 1);

    //....

    my_ptr_set. insert(&x );

 

    my_struct   y( 100);

    my_ptr_set. insert(&y );

    //.....

 

    // 错误,不能这样

    my_ptr_set_type:: iterator iter2 = my_ptr_set. find(100 );

 

    // 只能这样

    my_struct asKey(100 );

    my_ptr_set_type:: iterator iter1 = my_ptr_set. find(&asKey );

 

    // 或者这样

    my_ptr_set_type:: iterator iter3 = my_ptr_set. find(&my_struct (100));

}

 

// 如果 std::set::find 是个 template member function:

//    template iterator find(Key key) { ... }

// 那么 my_ptr_set.find(100) 就是合法的,并且不会增加创建一个对象的开销

- 作者: 傻馒头 2007年11月19日, 星期一 23:07  回复(0) |  引用(1) 加入博采

sizeof空类

class a{};

sizeof(a) is 1 byte.

- 作者: 傻馒头 2007年11月19日, 星期一 15:40  回复(0) |  引用(1) 加入博采

atoi & itoa
数字转字符串:
用C++的streanstream:
#include 
#Include 

string num2str(double i)
{
        stringstream ss;
        ss
<<i;
        
return ss.str();
}

字符串转数字:

int str2num(string s)
 
{   
        
int num;
        stringstream ss(s);
        ss
>>num;
        
return num;
}

上面方法很简便, 缺点是处理大量数据转换速度较慢..
C library中的sprintf, sscanf 相对更快

可以用sprintf函数将数字输出到一个字符缓冲区中. 从而进行了转换...
例如:
已知从0点开始的秒数(seconds) ,计算出字符串"H:M:S",  其中H是小时, M=分钟,S=秒
         int H, M, S;
        
string time_str;
        H
=seconds/3600;
        M
=(seconds%3600)/60;
        S
=(seconds%3600)%60;
        
char ctime[10];
        sprintf(ctime, 
"%d:%d:%d", H, M, S);             // 将整数转换成字符串
        time_str=ctime;                                                 // 结果 


与sprintf对应的是sscanf函数, 可以将字符串转换成数字
    char    str[] = "15.455";
    
int     i;
    
float     fp;
    sscanf( str, 
"%d"&i );         // 将字符串转换成整数   i = 15
    sscanf( str, "%f"&fp );      // 将字符串转换成浮点数 fp = 15.455000
    
//打印
    printf( "Integer: = %d ",  i+1 );
    printf( 
"Real: = %f ",  fp+1 ); 
    
return 0;


输出如下:
Integer: = 16
 Real: = 16.455000 


下面是msdn 8.0 关于sprintf函数
#include <stdio.h>

int main()
{
   
char  buffer[200], s[] = "computer", c = 'l';
   
int   i = 35, j;
   
float fp = 1.7320534f;

   
// Format and print various data: 
   j  = sprintf( buffer,     "   String:    %s", s ); // C4996
   j += sprintf( buffer + j, "   Character: %c", c ); // C4996
   j += sprintf( buffer + j, "   Integer:   %d", i ); // C4996
   j += sprintf( buffer + j, "   Real:      %f", fp );// C4996
   
// Note: sprintf is deprecated; consider using sprintf_s instead

   printf( 
"Output:%scharacter count = %d", buffer, j );
}


Output:
String: computer
Character: l
Integer: 35
Real: 1.732053

int sprintf(
char *buffer,
const char *format [,
argument] ...
);

buffer

Storage location for output

format

Format-control string

argument

Optional arguments


Return type
sprintf returns the number of bytes stored in buffer, not counting the terminating null character.

character count = 79

关于格式(format)

A format specification, which consists of optional and required fields, has the following form:

%[flags] [width] [.precision] [{h | l | ll | I | I32 | type

Flags:

The first optional field of the format specification is flags. A flag directive is a character that justifies output and prints signs, blanks, decimal points, and octal and hexadecimal prefixes. More than one flag directive may appear in a format specification.

Flag Characters
Flag Meaning Default

Left align the result within the given field width.

Right align.

+

Prefix the output value with a sign (+ or –) if the output value is of a signed type.

Sign appears only for negative signed values (–).

0

If width is prefixed with 0, zeros are added until the minimum width is reached. If 0 and appear, the 0 is ignored. If 0 is specified with an integer format (i, u, x, X, o, d) and a precision specification is also present (for example, %04.d), the 0 is ignored.

No padding.

blank (' ')

Prefix the output value with a blank if the output value is signed and positive; the blank is ignored if both the blank and + flags appear.

No blank appears.

#

When used with the o, x, or X format, the # flag prefixes any nonzero output value with 0, 0x, or 0X, respectively.

No blank appears.

 

When used with the e, E, f, a or A format, the # flag forces the output value to contain a decimal point in all cases.

Decimal point appears only if digits follow it.

 

When used with the g or G format, the # flag forces the output value to contain a decimal point in all cases and prevents the truncation of trailing zeros.

Ignored when used with c, d, i, u, or s.

Decimal point appears only if digits follow it. Trailing zeros are truncated.

width:

The second optional field of the format specification is the width specification. The width argument is a nonnegative decimal integer controlling the minimum number of characters printed. If the number of characters in the output value is less than the specified width, blanks are added to the left or the right of the values — depending on whether the – flag (for left alignment) is specified — until the minimum width is reached. If width is prefixed with 0, zeros are added until the minimum width is reached (not useful for left-aligned numbers).

The width specification never causes a value to be truncated. If the number of characters in the output value is greater than the specified width, or if width is not given, all characters of the value are printed


Character Type Output format

c

int or wint_t

When used with printf functions, specifies a single-byte character; when used with wprintf functions, specifies a wide character.

C

int or