How std::unordered_map organizes element storing and how elements are inserted into or searched in std::unordered_map
std::unordered_map
is organized into buckets. Imagine having an array where every cell is a bucket that contains elements. A question might arise from these words: “that contains elements.” Are we talking about giving an array as the second parameter in the following code?
#include <unordered_map>#include <vector> #include <string> int main() { std::unordered_map<std::string, std::vector<int>> table; table["Word"] = { 45,6,2,6 }; }
In this case, we see clearly that there is more than one value that has to be stored. Although this example might seem reasonable and a logical motivation to have buckets, it is not the case. The bucket an element is placed into depends entirely on the hash of its key. Two different keys with different values could generate the same hash (bucket). The bucket...