In std::map, what happens to the other iterators when one element is erased through an iterator?
Correct answer
D. Only the erased iterator is invalidated; every other iterator stays valid
19 views
D. Only the erased iterator is invalidated; every other iterator stays valid
19 views
std::map is a node-based red-black tree, so erasing one element frees only that node and leaves references and iterators to other elements valid. This is the key difference from std::vector, where erasing shifts elements and invalidates everything at or after the erased position. C++11 also made erase return the following iterator, enabling the it = m.erase(it) loop.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is D? Ask here and get an explanation.