|
|
用户名:shamantou 笔名:傻馒头 地区: 中国-上海 行业:其他 |
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
[转] 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
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
// 那么 my_ptr_set.find(100) 就是合法的,并且不会增加创建一个对象的开销
atoi & itoa
#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;
}
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; // 结果
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;#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] ...
);
Storage location for output
Format-control string
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 | 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, | 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 |