Saturday, 15 August 2015

c++ - std::map multiple iterators, deletion and its value -



c++ - std::map multiple iterators, deletion and its value -

#include <stdio.h> #include <iostream> #include <map> #include <string> #include <stdlib.h> using namespace std; class prepare { }; int main() { map<int, prepare *> m; prepare * f = new fix(); m.insert( make_pair( 2, f) ); m.insert( make_pair( 3, f) ); map<int, prepare *>::iterator = m.find(2); map<int, prepare *>::iterator it1 = m.find(2); m.erase(it); // create problem // m.erase(it1); // still value there // map node, iterator re-create value ? printf("%d\n", it->first); printf("%d\n", it1->first); }

i have map contains 2 entries, 2 iterators pointing same entry. erased 1 entry map using iterator1. post deletion still iterator1 , iterator2 hold value.

questions

is iterator pointing node of map ( reddish black tree) is iterator coping both key , value node while iterating ? because of holds value after entry deleted map.

for std::map::erase using this method on iterator has next effects:

removes specified elements container

references , iterators erased elements invalidated. other references , iterators not affected.

so cannot utilize it1 after erased it, though it1 can still point 'now invalid' previous memory coincidence.

c++ map stl iterator

No comments:

Post a Comment