今天写程序,如下:
[cpp]
set<CmdTask>::iterator itr = tasks.begin();
while (itr != tasks.end()) {
//找pid相同的
if (itr->pid == pid) {
break;
}
itr++;
}
if (itr != tasks.end()) {
//找到了
unsigned int *ptr = &(itr->state);
itr->state = TASK_STOP;
logger ->info("success handle exit process , expID(%ld) pid(%d).",
itr->expID, itr->pid);
return true;
} else {
//在set中没有找到,报错
logger->error(
"fail handle exit process ,not found in set<CmdTask>, expID(%ld) pid(%d).",
itr->expID, itr->pid);
return false;
}
[/cpp]
结果itr->state一句死活不能赋值,提示无法给const赋值……查了半天,原来set<Type>::iterator是set<Type>::const_iterator的别名,因为set是排序的,如果不是const,会导致赋值后修改了顺序而无法得知尽心排序。