unordered_map 中字符串的 C++ 哈希函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15595804/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Hash function for string in unordered_map
提问by MirroredFate
It seems as if C++ does not have a hash function for strings in the standard library. Is this true?
似乎 C++ 在标准库中没有用于字符串的哈希函数。这是真的?
What is a working example of using a string as a key in an unordered_map that will work with any c++ compiler?
在 unordered_map 中使用字符串作为键的工作示例是什么,它可以与任何 C++ 编译器一起使用?
采纳答案by awesoon
C++ STL provides template specializationsof std::hash
for the various string classes. You could just specify std::string
as key type for std::unordered_map
:
C ++ STL提供模板特的std::hash
用于各种字符串类。您可以指定std::string
为密钥类型std::unordered_map
:
#include <string>
#include <unordered_map>
int main()
{
std::unordered_map<std::string, int> map;
map["string"] = 10;
return 0;
}
回答by Dave
I ran into this today (actually with wstring
, not string
, but it's the same deal): using wstring
as a key in an unordered_map
generates an error about no hash function being available for that type.
我今天遇到了这个问题(实际上是wstring
,不是string
,但它是一样的):wstring
在 an 中用作键会unordered_map
产生一个关于没有可用于该类型的哈希函数的错误。
The solution for me was to add:
我的解决方案是添加:
#include <string>
Believe it or not, without the include I still had the wstring type available but apparently NOT the ancillary functions like the hash. Simply adding the include above fixed it.
信不信由你,如果没有包含,我仍然可以使用 wstring 类型,但显然不是像散列这样的辅助函数。只需添加上面的包含即可修复它。
回答by RiaD
Actually, there is std::hash<std::string>
实际上,有 std::hash<std::string>
But there it is how you can use another hash function:
但是你可以如何使用另一个哈希函数:
struct StringHasher {
size_t operator()(const std::string& t) const {
//calculate hash here.
}
}
unordered_map<std::string, ValueType, StringHasher>
回答by John Leidegren
If you have a CustomType
and you want to plug into the STL infrastructure this is what you could do.
如果你有一个CustomType
并且你想插入 STL 基础设施,这就是你可以做的。
namespace std
{
//namespace tr1
//{
// Specializations for unordered containers
template <>
struct hash<CustomType> : public unary_function<CustomType, size_t>
{
size_t operator()(const CustomType& value) const
{
return 0;
}
};
//} // namespace tr1
template <>
struct equal_to<CustomType> : public unary_function<CustomType, bool>
{
bool operator()(const CustomType& x, const CustomType& y) const
{
return false;
}
};
} // namespace std
If you then want to create say a std::unordered_map<CustomType>
the STL will find the hash
and equal_to
functions without you having to do anything more with the template. This is how I like to write my custom equality comparer that support unordered data structures.
如果您随后想创建一个std::unordered_map<CustomType>
,STL 将找到hash
和equal_to
函数,而您无需对模板执行任何其他操作。这就是我喜欢编写支持无序数据结构的自定义相等比较器的方式。
回答by sergiol
In my case it was really distraction.
就我而言,这真的是分心。
I had a type X for which I implemented hashing for const& Xan utilized it somewhere with
我有一个类型 X,我为它实现了const& X的散列,并在某处使用了它
std::unordered_map<const X, int> m_map;
Then I wanted to have another map which key are of the type X
and did:
然后我想要另一个地图,其中的键是该类型的X
并且做了:
std::unordered_map<X, int> map_x;
Notice the LACKof const
on the second case.
注意LACK的const
第二情况。