What requirements must a class meet to be used as a key in a std::map versus a std::unordered_map?

Last updated 21 June 2026

  • std::map (balanced BST, O(log n)) needs a strict weak ordering, by default operator< via std::less. Keys are equal when neither is less than the other.

  • std::unordered_map (hash table, average O(1)) needs a hash functor (std::hash specialization or custom hasher) plus equality (operator==).

  • In both, the key must stay effectively immutable while stored, since changing the ordered/hashed parts corrupts the container.

  • https://www.interviewbit.com/cpp-interview-questions/
  • https://cloud.tencent.com/developer/ask/sof/108114
All C++ interview questions