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
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)
;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With