1. Home
  2. Computer Science MCQs
  3. C++ and Object-Oriented Programming
  4. Which is the accurate comparison of std::map and…

Added 30 Nov -0001

Which is the accurate comparison of std::map and std::unordered_map?

  1. map keeps keys ordered with O(log n) operations; unordered_map gives average O(1) with no ordering and O(n) worst case Correct answer
  2. Both keep the keys ordered, differing only in hash quality, so iteration order is identical for both of the two containers
  3. unordered_map iterates in insertion order, unlike map, which re-sorts the keys at every single insertion
  4. map gives O(1) lookup while unordered_map is O(log n), because a balanced tree always beats a hash table

Correct answer

A. map keeps keys ordered with O(log n) operations; unordered_map gives average O(1) with no ordering and O(n) worst case

15 views

MCQSEXAM

Explanation

Associative container choice balances ordering against speed: std::map is a balanced tree, so find, insert and erase are O(log n) and iteration is sorted, while std::unordered_map hashes to average constant time at the cost of order and a worst case that degrades to linear under heavy collisions. Rehashing invalidates unordered_map iterators but keeps references and pointers to elements valid.

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