删除 en 元素后如何正确指向 std::list (双 for 循环)?
我想从 std::list
中删除一个元素,然后指向此列表,但是当我这样做时
for(std::list<CvRect>::iterator it = listOfCvRects.begin(); it != listOfCvRects.end(); it++)
{
for(std::list<CvRect>::iterator jt = listOfCvRects.begin(); jt != listOfCvRects.end(); jt++)
{
if( it == jt )
{ continue;}
if( (jt->x) > (it->x) //.. more conditions...)
{
jt = listOfCvRects.erase(jt);
//OR
//listOfCvRects.erase(jt++);
}
}
}
,我得到了未处理的异常:iterator is unincrementable
I want to erase an element from the std::list
and then point back to this list but when I do it this way
for(std::list<CvRect>::iterator it = listOfCvRects.begin(); it != listOfCvRects.end(); it++)
{
for(std::list<CvRect>::iterator jt = listOfCvRects.begin(); jt != listOfCvRects.end(); jt++)
{
if( it == jt )
{ continue;}
if( (jt->x) > (it->x) //.. more conditions...)
{
jt = listOfCvRects.erase(jt);
//OR
//listOfCvRects.erase(jt++);
}
}
}
I got and unhandled exception : iterator is unincrementable
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为问题在于,在某些情况下(删除元素的情况),您会双倍递增迭代器。你的 for 循环看起来像这样:
但在它的内部你正在做这样的事情:
所以,如果发生擦除的情况,你擦除它,同时,将迭代器设置为下一个元素......但是,您还可以使用
jt++
来增加它!解决这个问题的简单方法是稍微重写 for 循环以适应这种形式因素:
因此您正在执行一个或另一个增量,但绝不会同时执行两者。
I believe the problem is that in some cases (those where you delete an element), you are double incrementing the iterator. Your for loop looks like this:
But inside of it you are doing something like this:
So, if the case happens that you do the erase, you erase it and at the same time, set the iterator to the next element... But, you also increment it with the
jt++
!The simple approach to fixing this is re-write the for loop slightly to fit this form factor:
so you are doing one or the other increments, but never both.
从列表中删除一个元素会使指向该元素的迭代器无效,但不会使其他迭代器无效。所以你需要在擦除之前进行增量:
Erasing an element from a list invalidates iterators that point at that element, but not other iterators. So you need to do the increment before the erase: