Unordered_set insertion worst case

875 views Asked by At

I have a question about insertion in unordered_set. I want to build an example of worst case insertion. I have 30000 strings(len string <= 16, possible characters are english letters uppercase or lowercase and symbol "_"). So i have 53^16 words, and i have to choose 30000 string to build worst case insertion. I knew that unordered_set uses std::hash for hashing, and i tried to sort out words with len = 3, and calculate their hashes. I get, that each hash is different, so i cant build words with len = 3 with same hashes to build worst case insertion example? So what can i do?

1

There are 1 answers

4
Tony Delroy On

std::hash implementations vary - the Standard lets the compiler vendor do whatever they think sensible. So, it's easiest to get worst case behaviour by specifying your own hash routine when you instantiate the unordered_set: it can just return the same value (e.g. 0) for all input values.

struct Hash
{
    size_t operator()(const std::string&) const { return 0; }
};

std::unordered_set<string, Hash> my_set;