小编ssk*_*ssk的帖子

如何在 cpp 中为 std::unordered_map<T> 编写自定义 hash_function?

我想为 std::unordered_map 编写自己的 Hash_function 而不是使用默认函数。我可以在许多网站上找到 unordered_map::hash_function()。但是使用这个我只能得到生成的哈希值,使用这样的东西:

/*Sample map of strings*/

unordered_map<string, string> sample;

// inserts key and elements
sample.insert({ "Tom", "MNNIT" });
sample.insert({ "Kate", "MNNIT" });

unordered_map<string, string>::hasher foo
    = sample.hash_function();

cout << foo("Tom") << endl;
Run Code Online (Sandbox Code Playgroud)

但是我怎样才能有更多的控制权并创建我自己的散列函数版本?因此,例如,对于键“Tom”,我希望哈希值为 100。

c++ std

2
推荐指数
1
解决办法
75
查看次数

标签 统计

c++ ×1

std ×1