我声明一个 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>&)’
你们能帮我解释一下这个错误吗?非常感谢!
最佳答案
这是因为 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
提供您的自定义哈希函数。
关于c++ - 插入 unordered_map 时没有匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230556/