As I know, std::unordered_map is used for fast access to elements. This achieves by storing and comparing the key hash instead of the key itself. Also, unordered means that the elements in it are not sorted. But fast access to elements requires that the items have sorted to be able to find the requested item using binary search.
- Does it mean the items in an unordered_map are sorted based on their hash key and the only reason that causes an unordered_map to be faster than map to access elements is comparing hash values is usually much more faster than comparing key values?
- If so, choosing between unordered_map and map depends on the type of key. Am I right?
And the last question is why an unordered_map does not get a Compare template parameter like what the map does? How does an unordered_map compare the key hashes just by an equal operator?
template <class Key, class T, class Compare = less<Key>, class Alloc = allocator<pair<const Key,T> > > class map; template <class Key, class T, class Hash = hash<Key>, class Pred = equal_to<Key>, class Alloc = allocator< pair<const Key,T> > > class unordered_map;