C++ – std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值
本文作者:StubbornHuang
版权声明:本文为站长原创文章,如果转载请注明原文链接!
原文标题:C++ – std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值
原文链接:https://www.stubbornhuang.com/2467/
发布于:2023年01月05日 13:49:27
修改于:2023年01月05日 13:49:27
1 std::unordered_map中使用结构体或者vector等复杂的数据结构作为Key值
1.1 std::vector作为Key
C++11中引入了unordered_set和unordered_map,其内部数据结构实现没有使用map和set中的红黑树,而是使用的哈希表。如果我们在unordered_set和unordered_map使用比较复杂的数据结构,比如结构体,类,或者类似于std::vector<int>
这种作为Key,那么此时我们需要自定义Key的hash值,不然会出现编译错误,比如以下代码
#include <iostream>
#include <unordered_map>
#include <vector>
int main()
{
std::unordered_map<std::vector<int>, std::string> test;
return 0;
}
在VS中进行编译会出现以下的编译错误
错误C2280“std::_Uhash_compare<_Kty,_Hasher,_Keyeq>::_Uhash_compare(const std::_Uhash_compare<_Kty,_Hasher,_Keyeq> &)”: 尝试引用已删除的函数 C++BasicTest C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.29.30133\include\unordered_map 50
在上述代码中,我们将std::vector<int>
作为std::unorder_map
的Key值,而这种复杂的结构体我们需要自定义std::vector<int>
这个Key所使用的Hash,我们可以对上述代码进行如下修改
#include <iostream>
#include <unordered_map>
#include <vector>
struct Hash
{
std::size_t operator()(const std::vector<int>& prefix) const {
size_t hash_code = 0;
for (int id : prefix) {
hash_code = id + 77 * hash_code;
}
return hash_code;
}
};
int main()
{
std::unordered_map<std::vector<int>, std::string, Hash> test;
return 0;
}
这个时候再对上述代码进行编译就不会出现编译错误了。
1.2 struct作为Key
同样的,我们也可以将结构体作为std::unorder_map
的Key,我们也需要定义strcut的Hash值,一个简单的示例代码如下,
#include <iostream>
#include <unordered_map>
#include <vector>
struct Point {
int point_id;
float x;
float y;
float z;
};
struct PointHash
{
std::size_t operator()(const Point& point) const {
return point.point_id + (int)point.x * 3 + (int)point.y * 5 + (int)point.z * 7;
}
};
int main()
{
std::unordered_map<std::vector<int>, std::string, PointHash> test;
return 0;
}
当前分类随机文章推荐
- C++11 - 使用std::chrono计算程序、函数运行时间 阅读2780次,点赞0次
- C++ - vector存储动态指针时正确释放内存 阅读5894次,点赞0次
- C++ - 使用std::chrono获取当前秒级/毫秒级/微秒级/纳秒级时间戳 阅读3578次,点赞0次
- C++11 - 封装std::thread,增加子线程启动、暂停、唤起、停止功能 阅读4834次,点赞7次
- C++ - 求解std::vector
中topk数值以及topk数值对应的索引 阅读2475次,点赞0次 - C++ - std::string输出双引号到字符串 阅读3228次,点赞0次
- C++ - 使用宏区分不同系统平台、不同编译器、不同编译模式等编译期宏使用总结 阅读1397次,点赞0次
- C++ - 控制台程序在控制台窗口可变参数格式化带颜色输出日志信息 阅读3291次,点赞0次
- C++ - 格式化json字符串,方便展示json字符串的层次结构 阅读2977次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与double的相互转换 阅读2293次,点赞0次
全站随机文章推荐
- 资源分享 - Game Physics (First Edition) 英文高清PDF下载 阅读1613次,点赞0次
- 资源分享 - Introduction to 3D Game Programming with DirectX 11 英文高清PDF下载 阅读3479次,点赞0次
- 资源分享 - Mathematics for Computer Graphics , Third Edition 英文高清PDF下载 阅读1338次,点赞0次
- 工具网站推荐 - DLL‑FILES.COM帮你找到你的应用程序所缺失的dll文件 阅读2572次,点赞0次
- 资源分享 - Calculus for Computer Graphics , First Edition 英文高清PDF下载 阅读1173次,点赞0次
- 资源分享 - Game Engine Architecture (Second Edition)英文高清PDF下载 阅读2441次,点赞0次
- 资源分享 - Computer Graphics with OpenGL , Third Edition 英文高清PDF下载 阅读1109次,点赞0次
- C++11/std::atomic - 原子变量(不加锁实现线程互斥) 阅读6398次,点赞2次
- Sigmoid激活函数的快速替代函数以及相应的C++实现 阅读1618次,点赞0次
- 工具软件 - 开源文献管理软件Zotero的基础设置和插件安装 阅读389次,点赞0次
评论
169