A std::unordered_map is rehashed by an insertion. Which of its element references survive that operation?
Correct answer
A. References and pointers to elements stay valid, but the iterators are invalidated
14 views
A. References and pointers to elements stay valid, but the iterators are invalidated
14 views
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.