Remove element in unordered_set using iterator in a loop

别说谁变了你拦得住时间么 提交于 2021-01-27 15:07:08

问题


Please consider the following code:

Class MyClass is a self-defined class:

class MyClass
{
public:
    MyClass(int v) : Val(v) {}
    int Val;
};

Then the following code will cause Debug Assertion Failed in the loop just after calling it = T.erase(it);:

unordered_set<MyClass*> T;
unordered_set<MyClass*>::iterator it;

for (int i=0; i<10; i++)
    T.insert(new MyClass(i));

for (it = T.begin(); it != T.end(); it++)
{
    if ( (*it)->Val == 5 )
        it = T.erase(it); // After this line executes, in the next loop, the error occurs.
}

How to solve it and Why? PS: My environment: VS2010


回答1:


Suppose that the last element has Val = 5.

it = T.erase(it) is called, and it is set to T.end().

Then it++ is called, which causes an error because it is already set to end.

Essentially... when you erase an element in your current code, you end up double-advancing the iterator.

You could go with something like this instead....

for (it = T.begin(); it != T.end(); (*it)->Val == 5? it = T.erase(it) : ++it)
  ;



回答2:


This is what I usually do:

for (auto it = T.begin(); it != T.end(); )
{
    if ((*it)->value == 5) it = T.erase(it);
    else ++it;
}

This might increase readability if your erase condition gets more complex.



来源:https://stackoverflow.com/questions/23252386/remove-element-in-unordered-set-using-iterator-in-a-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!