map erase

hi everybody......

i have been trying to erase those pairs in map whose value is 0.......but the program would just stop working at that point.......
here is d short code.......

#include <map>

int main()
{
map<string,int> mapa;
map<string,int>::iterator p;
........
............

for(p=mapa.begin();p!=mapa.end();p++){
if((*p).second==0){
mapa.erase(p);
}
}
..............
..............
return 0;
}

ny help wud be greatly appreciated.
Last edited on
When you erase the iterator it will point at the next entry. So if the last one is 0 it'll go 1 past end and the for loop will fail causing a crash etc..

if ((*p).second == 0) {
mapa.erase(p);
p--;
continue;
}

is a dirty way to do it. I'd look at a better option myself.
Topic archived. No new replies allowed.