我一直在寻找与迭代器相同的问题;我发现了很多标题几乎相同、问题相似的主题,但我的有点不同。
我试图在特定位置获取 *iterator,但出现错误: “list iterator not deferencable”在运行时,这是我的代码:
CompetidorFormula2000* retornarCompetidorF2000(int pos){
list<Competidor*>::iterator itr=miLista.begin();
CompetidorFormula2000* f1=new CompetidorFormula2000();
if(pos>0 && pos<=miLista.size()){
advance(itr,pos);
}
f1=(CompetidorFormula2000*) (*itr);
return f1;
delete f1;
}
最佳答案
如果 pos
等于 miLista.size()
那么您将获得一个指向列表末尾的迭代器,与调用 miLista 时获得的迭代器相同.end()
。列表末尾的迭代器不可取消引用。
关于c++ - 列表迭代器不可取消引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11002140/