|
|
用户名:shamantou 笔名:傻馒头 地区: 中国-上海 行业:其他 |
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
[转] set
#include <set>
#include <iostream>
class A
{
public:
nbsp; A(int 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(/span>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 );
// C4996j += 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 wint_t | When used with printf functions, specifies a wide character; when used with wprintf functions, specifies a single-byte character. |
d | int | Signed decimal integer. |
i | int | Signed decimal integer. |
o | int | Unsigned octal integer. |
u | int | Unsigned decimal integer. |
x | int | Unsigned hexadecimal integer, using "abcdef." |
X | int | Unsigned hexadecimal integer, using "ABCDEF." |
e | double | Signed value having the form [ – ]d.dddd e [sign]dd[d] where d is a single decimal digit, dddd is one or more decimal digits, dd[d] is two or three decimal digits depending on the output format and size of the exponent, and sign is + or –. |
E | double | Identical to the e format except that E rather than e introduces the exponent. |
f | double | Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. The number of digits before the decimal point depends on the magnitude of the number, and the number of digits after the decimal point depends on the requested precision. |
g | double | Signed value printed in f or e format, whichever is more compact for the given value and precision. The e format is used only when the exponent of the value is less than –4 or greater than or equal to the precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. |
G | double | Identical to the g format, except that E, rather than e, introduces the exponent (where appropriate). |
a | double | Signed hexadecimal double precision floating point value having the form [−]0xh.hhhh p±dd, where h.hhhh are the hex digits (using lower case letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point. |
A | double | Signed hexadecimal double precision floating point value having the form [−]0Xh.hhhh P±dd, where h.hhhh are the hex digits (using capital letters) of the mantissa, and dd are one or more digits for the exponent. The precision specifies the number of digits after the point. |
n | Pointer to integer | Number of characters successfully written so far to the stream or buffer; this value is stored in the integer whose address is given as the argument. See Security Note below. |
p | Pointer to void | Prints the address of the argument in hexadecimal digits. |
s | String | When used with printf functions, specifies a single-byte–character string; when used with wprintf functions, specifies a wide-character string. Characters are printed up to the first null character or until the precision value is reached. |
S | String | When used with printf functions, specifies a wide-character string; when used with wprintf functions, specifies a single-byte–character string. Characters are printed up to the first null character or until the precision value is reached. |
Note If the argument corresponding to %s or %S is a null pointer, "(null)" will be printed.
Note In all exponential formats, the default number of digits of exponent to display is three. Using the _set_output_format function, the number of digits displayed may be set to two, expanding to three if demanded by the size of exponent.
Security Note The %n format is inherently insecure and is disabled by default; if %n is encountered in a format string, the invalid parameter handler is invoked as described in Parameter Validation. To enable %n support, see _set_printf_count_output.
Precision:
printf( "%.0d", 0 ); /* No characters output */
| Type | Meaning | Default |
|---|---|---|
a, A | The precision specifies the number of digits after the point. | Default precision is 6. If precision is 0, no point is printed unless the # flag is used. |
c, C | The precision has no effect. | Character is printed. |
d, i, u, o, x, X | The precision specifies the minimum number of digits to be printed. If the number of digits in the argument is less than precision, the output value is padded on the left with zeros. The value is not truncated when the number of digits exceeds precision. | Default precision is 1. |
e, E | The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded. | Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed. |
f | The precision value specifies the number of digits after the decimal point. If a decimal point appears, at least one digit appears before it. The value is rounded to the appropriate number of digits. | Default precision is 6; if precision is 0, or if the period (.) appears without a number following it, no decimal point is printed. |
g, G | The precision specifies the maximum number of significant digits printed. | Six significant digits are printed, with any trailing zeros truncated. |
s, S | The precision specifies the maximum number of characters to be printed. Characters in excess of precision are not printed |
EVENT & Multi-Thread
事件分为两类:人工重置, 自动重置
人工重置时,所有等待线程都变为有信号状态。并且持续保持有信号状态,除非显式调用ResetEvent(g_hEvent);
自动重置则不。所以人工重置不利于做线程同步。
创建事件 g_hEvent = CreateEvent(NULL, false, false, NULL);
param1:安全级别,设置为NULL, 取得默认值
param2:人工重置(true), 自动重置(false)
param3:事件状态 有信号(true) 无信号(false)
param4:事件名称,如果匿名设置为NULL
一般采用自动重置方式。如果事件状态如果设置为false要使用SetEvent(g_hEvent);设置为有信号
设置为有信号 SetEvent(g_hEvent);
设置为无信号 ReSetEvent(g_hEvent);
#i nclude <windows.h>
#i nclude <iostream.h>
extern int tickets;
//事件对象
HANDLE g_hEvent;
DWORD WINAPI thread_Event_Fun1Proc(
LPVOID lpParameter)
{
while (true) {
WaitForSingleObject(g_hEvent, INFINITE); //取得事件对象
if (tickets>0) {
Sleep(1);
cout<<"thread1: "<<tickets--<<endl;
}
else
break;
SetEvent(g_hEvent); //设置为有信号
}
return 0;
}
DWORD WINAPI thread_Event_Fun2Proc(
LPVOID lpParameter)
{
while (true) {
WaitForSingleObject(g_hEvent, INFINITE); //取得事件对象
if (tickets>0) {
cout<<"thread2: "<<tickets--<<endl;
}
else
break;
SetEvent(g_hEvent); //设置为有信号
}
return 0;
}
void thread_Event()
{
HANDLE handle1 = CreateThread(NULL, 0, thread_Event_Fun1Proc, NULL, 0, NULL);
HANDLE handle2 = CreateThread(NULL, 0, thread_Event_Fun2Proc, NULL, 0, NULL);
CloseHandle(handle1);
CloseHandle(handle2);
//创建事件对象
//第2参数指设置为非人工重置(false) (注意不要使用人工方式,比较麻烦)
//第3参数指设置为无信号(false)
g_hEvent = CreateEvent(NULL, false, false, NULL);
//设置为有信号
SetEvent(g_hEvent);
//设置为无信号
//ReSetEvent(g_hEvent);
Sleep(200);
CloseHandle(g_hEvent);
}
void main()
{
thread_Event();
cout<<"++Event++"<<endl;
}
命名事件对象
void thread_Event()
{
HANDLE handle1 = CreateThread(NULL, 0, thread_Event_Fun1Proc, NULL, 0, NULL);
HANDLE handle2 = CreateThread(NULL, 0, thread_Event_Fun2Proc, NULL, 0, NULL);
CloseHandle(handle1);
CloseHandle(handle2);
//创建事件对象
//第2参数指设置为非人工重置(false) (注意不要使用人工方式,比较麻烦)
//第3参数指设置为无信号(false)
g_hEvent = CreateEvent(NULL, false, false, "tickets");
//程序只能有一个实例
if(g_hEvent)
{
if (ERROR_ALREADY_EXISTS == GetLastError()) {
cout<<"app alreadly run."<<endl;
return;
}
}
//设置为有信号
SetEvent(g_hEvent);
//设置为无信号
//ReSetEvent(g_hEvent);
Sleep(200);
CloseHandle(g_hEvent);
}
上面程序建立命名事件,在多个进程中可以使用。上面就是在一个进程中判断命名事件对象是否已经存在,如果存在,就拒绝线程继续执行。
这样。当两个线程同时运行时,就会提示已经有一个线程运行。
//事件
#i nclude <windows.h>
#i nclude <iostream.h>
extern int tickets;
//临界区对象
HANDLE g_hEvent;
DWORD WINAPI thread_Event_Fun1Proc(
LPVOID lpParameter)
{
while (true) {
WaitForSingleObject(g_hEvent, INFINITE); //取得事件对象
if (tickets>0) {
Sleep(1);
cout<<"thread1: "<<tickets--<<endl;
}
else
break;
SetEvent(g_hEvent); //设置为有信号
}
return 0;
}
DWORD WINAPI thread_Event_Fun2Proc(
LPVOID lpParameter)
{
while (true) {
WaitForSingleObject(g_hEvent, INFINITE); //取得事件对象
if (tickets>0) {
cout<<"thread2: "<<tickets--<<endl;
}
else
break;
SetEvent(g_hEvent); //设置为有信号
}
return 0;
}
void thread_Event()
{
HANDLE handle1 = CreateThread(NULL, 0, thread_Event_Fun1Proc, NULL, 0, NULL);
HANDLE handle2 = CreateThread(NULL, 0, thread_Event_Fun2Proc, NULL, 0, NULL);
CloseHandle(handle1);
CloseHandle(handle2);
//创建事件对象
g_hEvent = CreateEvent(NULL, false, false, NULL);
//设置为有信号
SetEvent(g_hEvent);
Sleep(200);
}
void main()
{
thread_Event();
cout<<"++Event++"<<endl;
}
HANDLE CreateEvent(
LPSECURITY_ATTRIBUTES lpEventAttributes, 安全选项
BOOL bManualReset, true:人工方式(一般不要使用), false:自动方式
BOOL bInitialState, true:初始有信号, false:初始无信号
LPTSTR lpName); 事件名字,如果匿名为NULL
BOOL bManualRest
如果设置事件为人式方式,则事件对所有线程表现为有信号状态,如果为自动方式,对所有线程表现为无信号状态
如果设置为人工方式,
在WaitForSingleObject(g_hEvent, INFINITE); 后调用 ResetEvent(g_hEvent)设置为无信息状态,但如果在两句之间切换CPU时间,仍然达不到同步的目的。所以在做线程同步时不要用人式方式。
如果设置为自动方式,
WaitForSingleObject(g_hEvent, INFINITE); 后则表现为无信号状态,在使用完资源后,再调用SetEvent(g_hEvent);设置为有信息状态,释放资源。
BOOL bInitialState,
如果初始设置为false, 为无信息状态, 在事件创建完成后,可以调用SetEvent设置为有信息状态,以让线程函数调用
BOOL SetEvent(
HANDLE hEvent );
设置为有信号状态
BOOL ResetEvent(
HANDLE hEvent );
设置为无信号状态
填写简历咋就嘎么麻烦
在线填报中国银行职位,刚写了一大段材料,一提交发现没有成功,再返回发现什么都没了,又得重新来。连续发生三次了,怒了。
chinahr的那个晚上简历投递系统设计的一点都不人性化。 好影响我写简历的心情,有点怒了!
:%&……&%¥