# Boost unordered_map problem

## Recommended Posts

Hi everyone, I am having a prolem erasing item from Boosst unordered_map using an iterator. The code is as following

for(boost::unordered_map::iterator it = map.begin();it != map.end();)
{
if (it.second != 10)
{
map.erase(it);
}
else
{
++it;
}
}


I run above code with one element in the map. When that element is erased from the map. i can see my map size is 0, but the iterator doesnt equal to end. So in the next loop, it will try erase an invalid iterator from an empty map, which causes the app to crash. I understand it might be something to do with unordered_map not being a sequenced structure, but what should I do to fix the problem? Thanks a lot in advance.

##### Share on other sites
Ohforf sake    2052
Doesn't it = map.erase(it) work?