Before considering a duplicate, please understand the basis of my question.
Why does a C++ std::map accept a std::pair as a key type, but a std::unordered_map does not?
The first case compiles perfectly:
#include <map> #include <utility> using namespace std; typedef pair<int,int> int_pair; int main() { map<int_pair,int> m; return 0; } The second case gives a ton of compile errors. It has become clear from this SO question and this SO question that a custom hash function and equivalence operator must be created.
#include <unordered_map> #include <utility> using namespace std; typedef pair<int,int> int_pair; int main() { unordered_map<int_pair,int> m; return 0; } The question here is not how to write the hash function for the std::unordered_map. The question is, why one is needed at all when std::map doesn't require one?
I know std::map is a Binary Search Tree (BST), but how exactly are the comparisons being made for that matter between keys of a non-fundamental type (int_pair)?
std::maprequiresstd::less<T>whereasstd::unordered_maprequiresstd::hash<T>, simple as that. As to "why" simply look up how to implement a red-black tree (map) vs a hash table (unordered_map), then you'll have your answer.std::mapcompiles becausestd::pairhas anoperator<which is called bystd::lesssee: en.cppreference.com/w/cpp/utility/pair/operator_cmp