1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. A std::unordered_map is rehashed by an insertion. Which of…

Added 30 Nov -0001

A std::unordered_map is rehashed by an insertion. Which of its element references survive that operation?

  1. References and pointers to elements stay valid, but the iterators are invalidated Correct answer
  2. Nothing survives: all iterators, pointers and references are invalidated at once
  3. Only iterators survive, because buckets are relinked while the nodes are reallocated
  4. Everything survives, since rehashing only changes the size of the bucket array

Correct answer

A. References and pointers to elements stay valid, but the iterators are invalidated

14 views

MCQSEXAM

Explanation

Rehashing rebuilds the bucket array but the nodes are not relocated, so references and pointers to elements remain valid while iterators are invalidated because their bucket sequence changes. Erasing an element invalidates only iterators to that element. std::map differs again: insertion never invalidates any iterator, which matters when erasing during iteration.

Reference

Asked in: NTS, PPSC IT, Campus Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? 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