【发布时间】:2021-05-04 17:48:09
【问题描述】:
我想为 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;
但是我怎样才能拥有更多的控制权并创建我自己的散列函数版本呢?因此,例如,让我们说键“Tom”,我希望哈希值为 100。
【问题讨论】:
-
使用像GNU emacs这样好的源代码编辑器并阅读this C++ reference和你的C++编译器(例如GCC)和调试器(例如GDB)的文档
-
阅读有关hash table、Introduction to algorithms 的维基百科,并用几段书面英语解释为什么您要求
"Tom"的哈希码应为100。在您的文件中提供一些minimal reproducible example问题。如果"Tom"的哈希码是not 100,你会怎样?另请阅读documentation ofstd::unordered_map