【发布时间】:2012-10-20 07:51:15
【问题描述】:
我得到这个奇怪的问题是代码 sn-p 工作正常。
std::multimap<long,int>::iterator dateItr = reconnQueueDates.begin();
while( dateItr!=reconnQueueDates.end() ){
LOG_DEBUG("RUN comparision " <<cDateTime<< ", "<<dateItr->first);
if(dateItr->first <= cDateTime){
long nextTimeout = -1;
if( (nextTimeout = callReconnects(dateItr->second,dateItr->first))>-1){
if(nextTimeout>0){
reconnQueueDates.insert(std::pair<long , int>(nextTimeout, dateItr->second));
}
reconnQueueDates.erase(dateItr);
LOG_DEBUG("modified the iterator ressetting");
dateItr = reconnQueueDates.begin();
LOG_DEBUG("resset iter");
}//end of callreconnect if
}else{
++dateItr;
} //else for datetime check
}//end of while
在此之前,我在循环中使用了带有 ++dateItr 的 for 循环,如下所示
for( ;dateItr!=reconnQueueDates.end();++dateItr ){
LOG_DEBUG("RUN comparision " <<cDateTime<< ", "<<dateItr->first);
if(dateItr->first <= cDateTime){
long nextTimeout = -1;
if( (nextTimeout = callReconnects(dateItr->second,dateItr->first))>-1){
if(nextTimeout>0){
reconnQueueDates.insert(std::pair<long , int>(nextTimeout, dateItr->second));
}
reconnQueueDates.erase(dateItr);
LOG_DEBUG("modified the iterator ressetting");
dateItr = reconnQueueDates.begin();
LOG_DEBUG("resset iter");
}// callReconnect
} // check datetime
}// for loop
在调试时,我发现在循环内更改映射后,for 构造内的迭代器值仍在使用旧地址。
我正在使用 ubuntu 12.04 和 g++ 版本 4.6.3。在我看来,这是某种编译器错误或某种优化。
知道它可能是哪个标志或错误。
【问题讨论】:
-
你必须展示你之前的完整循环(那个不工作的循环)。您在此处的正文与您在此处的(不完整的)
for不兼容。 -
您的问题/问题到底是什么?这是否与
reconnQueueDates.insert()在reconnQueueDates.erase()调用之后的调用使用了无效的迭代器这一事实有关? -
代码中也存在一些逻辑错误。但我的问题是,为什么迭代器会进入无限循环。使用 for 循环时。虽然我正在使用 map.begin 更新迭代器的值。