插入unordered_map时没有匹配的功能

kha*_*h89 4 c++ hash boost unordered-map

我声明unordered_map如下:

boost::unordered_map<std::array<char, 20>, t_torrent> torrent_ins;
Run Code Online (Sandbox Code Playgroud)

然后在其中插入一个元素(如果该键不存在,此映射将返回新元素的引用)

t_torrent& torrent_in = torrent_ins[to_array<char,20>(in)];
Run Code Online (Sandbox Code Playgroud)

但是我收到了一条错误消息:

../src/Tracker/torrent_serialization.cpp:30:   instantiated from here/usr/local/include/boost/functional/hash/extensions.hpp:176: error: no matching function    for call to ‘hash_value(const std::array<char, 20ul>&)’
Run Code Online (Sandbox Code Playgroud)

你能帮我解释一下这个错误吗?非常感谢!

Mar*_*cia 8

这是因为没有"默认"散列函数std::array<char, 20>,至少没有实现提供的函数.您必须提供散列函数,std::array<char, 20>然后才能使代码正常工作.

正如您在std :: unordered_map中看到的那样:

template<
    class Key,
    class T,
    class Hash = std::hash<Key>,
    class KeyEqual = std::equal_to<Key>,
    class Allocator = std::allocator< std::pair<const Key, T> >
> class unordered_map;
Run Code Online (Sandbox Code Playgroud)

您必须提供Hash类型Key以提供自定义哈希函数.