4

我声明unordered_map如下:

boost::unordered_map<std::array<char, 20>, t_torrent> torrent_ins;

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

t_torrent& torrent_in = torrent_ins[to_array<char,20>(in)];

但我收到一条错误消息:

../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>&)’

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

4

1 回答 1

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;

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

于 2013-01-09T07:56:34.390 回答