简体   繁体   English

unordered_map 中字符串的 C++ 哈希函数

[英]C++ Hash function for string in unordered_map

It seems as if C++ does not have a hash function for strings in the standard library.似乎 C++ 在标准库中没有字符串的哈希函数。 Is this true?这是真的?

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++ 编译器一起使用?

C++ STL provides template specializations of std::hash for the various string classes. C ++ STL提供的模板特化std::hash的各种串类。 You could just specify std::string as key type for std::unordered_map :您可以将std::string指定为std::unordered_map键类型:

#include <string>
#include <unordered_map>

int main()
{
    std::unordered_map<std::string, int> map;
    map["string"] = 10;
    return 0;
}

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 ,但它是一样的):在unordered_map使用wstring作为键会产生一个关于没有哈希函数可用于该类型的错误。

The solution for me was to add:我的解决方案是添加:

#include <string>

Believe it or not, without the #include directive I still had the wstring type available but apparently NOT the ancillary functions like the hash.信不信由你,如果没有#include指令,我仍然可以使用wstring类型,但显然不是像散列这样的辅助函数。 Simply adding the include above fixed it.只需添加上面的包含即可修复它。

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>

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.如果您随后想创建一个std::unordered_map<CustomType> ,STL 将找到hashequal_to函数,而您无需对模板执行任何操作。 This is how I like to write my custom equality comparer that support unordered data structures.这就是我喜欢编写支持无序数据结构的自定义相等比较器的方式。

In my case it was really distraction.就我而言,这真的很让人分心。

I had a type X for which I implemented hashing for const& X an 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 LACK of const on the second case.注意第二种情况下const缺失

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

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