c++ - 插入 unordered_map 时没有匹配函数

标签 c++ boost hash unordered-map

我声明一个 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/

相关文章:

c++ - 如何在不使用 C++ OpenGL SuperBible 着色器的情况下使用变换矩阵?

boost - 在 C++11 中的 boost::asio 套接字对象上重复 std::move

c++ - 链表与哈希表中的开放寻址

javascript - jsSHA512 来自同一字符串的不同哈希值

c++ - 在有限范围内有效合并两个列表

c++ - 使用过剩加载 ppm 文件

c# - 如何 Hook MFC CWnd 创建

c++ - 如何使boost ptree以相同的方式解析xml和json?

c++ - 为什么 boost 如此大量地模板化?

java - 使用 shiro 散列和加盐密码