简体   繁体   中英

Unordered_set insertion worst case

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?

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 (eg 0) for all input values.

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

std::unordered_set<string, Hash> my_set;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM