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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 19:31:21  来源:igfitidea点击:

C++ Hash function for string in unordered_map

c++stringc++11keyunordered-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::hashfor the various string classes. You could just specify std::stringas 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 wstringas a key in an unordered_mapgenerates 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 CustomTypeand 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 hashand equal_tofunctions 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 将找到hashequal_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 Xand did:

然后我想要另一个地图,其中的键是该类型的X并且做了:

std::unordered_map<X, int> map_x;

Notice the LACKof conston the second case.

注意LACKconst第二情况。