"Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average)."
So basically a hash function operates on the data sent to it and creates a "bucket (maybe a container)" for that hash value so if other data has the same hash value it can be stored in the bucket? This is called a hash table right?
Also how does the key identify the data if it is stored in a hash table with the hash value identifying it? Is the data stored as a pair? Or is the key actually turned into the hash value?
Also what advantage does this give over other containers in speed and efficiency? Actually not just what, but why?
Also what advantage does this give over other containers in speed and efficiency?
Hash tables have average O(1) complexity while accessing element by its key, inserting and erasing elements. Normal map (usually created using red-black trees) have O(log(n)) complexity in such cases.
Also how does the key identify the data if it is stored in a hash table with the hash value identifying it?
The hash value identifies a bucket. Hash values have a low but non-zero probability of collision (two key values with the same hash). Only when a collision occurs, is the Pred argument is called to determine if key values are the same. How unique keys with the same hash value are maintained is implementation specific, but is commonly implemented as a list anchored by the hash bucket.
Is the data stored as a pair?
Yes. Each list entry is a key-value pair.
Or is the key actually turned into the hash value?
The key is used to compute the hash, but the key must still be maintained since the hash is not guaranteed to be unique.
Also what advantage does this give over other containers in speed and efficiency?
The advantage over a standard map is that inserts and retrievals are very fast compared to a standard map. No ordering is required. In a standard map, each node of the tree must be traversed on insert and retrieval by value and the key compared at each node. With an unordered_map, the only time a comparison is done is when there is a hash collision.
Yes, I was referring to std::pair, although that would be implementation dependent.
So like in each bucket it is stored as a pair
Not quite. Each bucket contains a std::list<pair<keyT,valT>>. Remember that a bucket does not uniquely identify a key. For example on an insert, the list must be run to ensure the key being inserted is not a duplicate. Likewise on a find, the list must also be run to find a match on the key.
unordered_map has bucket per hash and a list in each bucket just in case that two keys have same hash. If you search for a key, you get at most one hit.
unordered_multimap very similar, except it expects to have multiple entries with same key in a list. If you search for a key, you can get multiple answers.
Algorithms differ, but datastructure does not need to.