1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. In std::map, what happens to the other iterators when one…

Added 30 Nov -0001

In std::map, what happens to the other iterators when one element is erased through an iterator?

  1. All iterators are invalidated, because the tree rebalances its nodes
  2. Iterators to later elements are invalidated, while earlier ones stay valid
  3. Iterators become valid again after the next insert into the container
  4. Only the erased iterator is invalidated; every other iterator stays valid Correct answer

Correct answer

D. Only the erased iterator is invalidated; every other iterator stays valid

19 views

MCQSEXAM

Explanation

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.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is D? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More C++ and Object-Oriented Programming MCQs

Practise all C++ and Object-Oriented Programming MCQs

Similar MCQs