c++debug错误 Expression: map/set iterator not incrementable

kiliwalk

关注

阅读 55

2022-09-19


刷题在devC上直接崩死,用vs打断点结果报错 Expression: map/set iterator not incrementable

c++debug错误 Expression: map/set iterator not incrementable_数组

代码如下:

map<string, Record>::iterator it;
for (it = parted.begin();it != parted.end();it++) {
string id = it->second.plateNum;
bool flag = false;
for (int i = lastIndex;i<N;i++) {
if (rec[i].plateNum == id) {
if (rec[i].statue == "in") break;
else {//下面有紧挨着的out与之配对
flag = true;
break;
}
}
}

if (flag == false) {
parted.erase(id);//配对失败的丢掉 循环过程中删除了map 那么it++会报错
}
}

原因在于遍历map的同时在循环体内修改了map,此处删除了map中的键值对,于是迭代器便乱了.it++的指向非法了

c++debug错误 Expression: map/set iterator not incrementable_迭代器_02

此处用string数组把该删的key记录下来,遍历完再删即可解决

精彩评论(0)

0 0 举报