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::shared_ptr - 循环引用问题 阅读3856次,点赞0次
- C++11 - 构建一个符合实际应用要求的线程池 阅读977次,点赞0次
- C++11 - override关键字简要介绍 阅读1920次,点赞0次
- C++11/std::thread - 线程管理join/detach 阅读2101次,点赞0次
- C++ - 使用宏区分不同系统平台、不同编译器、不同编译模式等编译期宏使用总结 阅读1107次,点赞0次
- C++ - int转string方法总结 阅读5591次,点赞0次
- C++ - Map中存储动态指针时正确释放内存 阅读3473次,点赞0次
- C++11 - 使用std::thread,std::shared_future,std::promise并行化/多线程化for循环,提升处理速度 阅读1309次,点赞0次
- C++ - 判断两个字符串是否相等方法总结 阅读168次,点赞0次
- C++11 - 使用std::codecvt进行字符编码转换需要注意的时间效率问题 阅读1575次,点赞1次
全站随机文章推荐
- 资源分享 - Artificial Intelligence - A Modern Approach , Fourth Edition 英文高清PDF下载 阅读3472次,点赞0次
- 资源分享 - GPU Pro 360 - Guide to GPGPU 英文高清PDF下载 阅读1955次,点赞0次
- 资源分享 - OpenGL Insights 英文高清PDF下载 阅读2426次,点赞0次
- C++ - std::map - 存储动态指针时正确释放内存 阅读3901次,点赞1次
- 如何回看PLTV/m3u8直播源 阅读241次,点赞0次
- 资源分享 - The Ray Tracer Challenge - A Test-Driven Guide to Your First 3D Renderer 英文高清PDF下载 阅读1500次,点赞0次
- 资源分享 - Image Content Retargeting - Maintaining Color, Tone, and Spatial Consistency 英文高清PDF下载 阅读1097次,点赞0次
- C++ - 字节数组byte[]或者unsigned char[]与long的相互转换 阅读738次,点赞0次
- 资源分享 - Ray Tracing in One Weekend英文高清PDF下载 阅读2504次,点赞0次
- 资源分享 - Computer Graphics with OpenGL , Third Edition 英文高清PDF下载 阅读858次,点赞0次
评论
164