is it invalid..
HI ,
Is it invalid ,, and lead to valgrind related issues ..
if yes, please suggest a alternative ..
map<string,A*>::iterator start = _maps->begin();
map<string,A*>::iterator end = _maps->end();
for( ; start != end ; start++)
{
string Name = (*start).first;
if(Name == "me")
{
_maps->erase(Name);
}
}
Regards..
map<string, A*>::iterator it = _maps->find( "me" );delete it->second; // Does _maps own the A* pointers? If so, you need to delete it before removing it from the map_maps->erase( it );
Yes that is invalid -- you're erasing the element pointed to by the iterator, which makes the iterator invalid.
After calling erase, the next call to "start++" (which should be "++start" by the way) will cause bad things to happen (because "start" is no longer a valid iterator).
Try:[Edit]If you're trying to find/erase a specific item, then you should use the find function like _fastcall posted above instead of writing the loop yourself ;)
After calling erase, the next call to "start++" (which should be "++start" by the way) will cause bad things to happen (because "start" is no longer a valid iterator).
Try:
map<string,A*>::iterator start = _maps->begin();map<string,A*>::iterator end = _maps->end();while( start != end ){ map<string,A*>::iterator current = start; ++start; if(current->first == "me") { _maps->erase(current); }}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement