Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
std::list::erase(it)
std::list::erase(it)
std::list::erase(it)

deletes the element the iterator points to and returns an iterator that points to the element that comes AFTER the deleted one, so this “advancement of the iterator” needs to be taken care of in the loop

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
bool advance = true;
for(auto it = container.begin(); it != container.end(); advance ? it++ : it)
{
advance = true;
doStuff();
if(someCondition)
{
it = container.erase(it);
advance = false;
}
doMoreStuff();
}
}
{ bool advance = true; for(auto it = container.begin(); it != container.end(); advance ? it++ : it) { advance = true; doStuff(); if(someCondition) { it = container.erase(it); advance = false; } doMoreStuff(); } }
{
    bool advance = true;
    for(auto it = container.begin(); it != container.end(); advance ? it++ : it)
    {
        advance = true;
        doStuff();
        if(someCondition)
        {
            it = container.erase(it);
            advance = false;
        }
        doMoreStuff();
    }
}