Explain the difference between std::map and std::unordered_map. What are the time complexities for their operations, and how do they handle collisions?

Last updated 21 June 2026

  • std::map is a sorted balanced tree (typically red-black) needing operator<, with O(log n) insert/find/erase.

  • std::unordered_map is an unordered hash table with O(1) average but O(n) worst-case operations. Collisions use separate chaining (per-bucket lists), and rehashing invalidates iterators.

  • Choose std::map for ordered traversal or range queries, std::unordered_map for fastest average lookups.

  • https://utkrusht.ai/blog/cplusplus-interview-questions
  • https://roadmap.sh/questions/cpp
  • https://internshala.com/blog/cpp-coding-interview-questions/
All C++ interview questions