Which is the accurate comparison of std::map and std::unordered_map?
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
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
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.
Asked in: NTS, PPSC IT, Campus Interviews
Not sure why the answer is A? Ask here and get an explanation.