在unordered_map中字符串的c++散列函数。

时间:2021-11-09 18:51:59

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中使用字符串作为键的工作示例是什么?

5 个解决方案

#1


27  

C++ STL provides template specializations of 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;
}

#2


16  

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 I still had the wstring type available but apparently NOT the ancillary functions like the hash. Simply adding the include above fixed it.

信不信由你,如果没有包含,我仍然有可用的wstring类型,但是显然没有像散列那样的辅助函数。简单地添加上面的include就可以了。

#3


13  

Actually, there is std::hash<std::string>

实际上,std::哈希< 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>

#4


7  

If you have a CustomType and you want to plug into the STL infrastructure this is what you could do.

如果你有一个定制类型,你想要插入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 , STL将找到散列和equal_to函数,而不需要对模板做任何操作。这就是我喜欢编写支持无序数据结构的自定义等式比较器的方式。

#5


0  

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。

#1


27  

C++ STL provides template specializations of 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;
}

#2


16  

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 I still had the wstring type available but apparently NOT the ancillary functions like the hash. Simply adding the include above fixed it.

信不信由你,如果没有包含,我仍然有可用的wstring类型,但是显然没有像散列那样的辅助函数。简单地添加上面的include就可以了。

#3


13  

Actually, there is std::hash<std::string>

实际上,std::哈希< 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>

#4


7  

If you have a CustomType and you want to plug into the STL infrastructure this is what you could do.

如果你有一个定制类型,你想要插入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 , STL将找到散列和equal_to函数,而不需要对模板做任何操作。这就是我喜欢编写支持无序数据结构的自定义等式比较器的方式。

#5


0  

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。