I usually wouldn't ask in such an straight-forward way as I'm about to do, but I've throughly reviewed my code, and with my current knowledge I can't see the error... The following code just freezes, apparently because of the 2nd block that erases from the list...
iterators become invalid as soon as you erase them. So if you do this:
1 2 3 4
for(; i != foo.end(); ++i)
{
foo.erase(i);
}
Erasing invalidates i, then when you do ++i, you're incrementing an invalid iterator.
Loops that erase elements should be structured like so:
1 2 3 4 5 6 7 8 9 10 11
while(i != foo.end())
{
if( need_to_erase )
{
i = foo.erase(i); // reassign i. Do not increment
}
else
{
++i; // only increment if you did not erase
}
}
Oh yes, I can see it now. it seems pretty obvious now, I read the theory but I didn't understand it. I appreciate your guidance a lot !
Now, I know this is probably asking a lot but, do you think you can show me how to do it with a for loop ? (if it's possible of course)
A for loop wouldn't be practical in this situation because the increment condition isn't fixed. Sometimes you increment and sometimes you don't... so using for doesn't really make sense. You're better off using while.
The only way to do it reasonably would be to make it look exactly like a while loop:
1 2 3 4 5 6 7 8 9 10 11 12
// while(i != foo.end()) // instead of this...
for(; i != foo.end();) // do this
{
if( need_to_erase )
{
i = foo.erase(i); // reassign i. Do not increment
}
else
{
++i; // only increment if you did not erase
}
}