Thursday, April 4, 2013

How to loop through a std::map and deleting entries using iterator in C++

Once you call map.erase(iterator) the iterator will be invalidated. What you need to do is something like:

for(iterator=map.begin(); iterator != map.end(); )
{
    del_iterator = iterator;
    iterator++;
    map.erase(del_iterator);
}

No comments:

Post a Comment