简体   繁体   English

Unordered_set插入最坏的情况

[英]Unordered_set insertion worst case

I have a question about insertion in unordered_set. 我有一个关于在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 "_"). 我有30000个字符串(len字符串<= 16,可能的字符是英文字母大写或小写,符号“_”)。 So i have 53^16 words, and i have to choose 30000 string to build worst case insertion. 所以我有53 ^ 16个单词,我必须选择30000字符串来构建最坏情况插入。 I knew that unordered_set uses std::hash for hashing, and i tried to sort out words with len = 3, and calculate their hashes. 我知道unordered_set使用std :: hash进行散列,我试图用len = 3来整理单词,然后计算它们的散列值。 I get, that each hash is different, so i cant build words with len = 3 with same hashes to build worst case insertion example? 我知道,每个哈希都是不同的,所以我不能用len = 3构建单词,使用相同的哈希来构建最坏情况的插入示例? So what can i do? 那我该怎么办?

std::hash implementations vary - the Standard lets the compiler vendor do whatever they think sensible. std::hash实现各不相同 - 标准允许编译器供应商做他们认为合理的事情。 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. 因此,通过在实例化unordered_set时指定自己的哈希例程来最简单地获得最坏情况:它可以为所有输入值返回相同的值(例如0)。

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

std::unordered_set<string, Hash> my_set;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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