C++ unordered_map fail when used with a vector as key(C++ unordered_map 在使用向量作为键时失败)
问题描述
背景:我来自 Java 世界,我对 C++ 或 Qt 相当陌生.
Background: I am comming from the Java world and I am fairly new to C++ or Qt.
为了玩转unordered_map,我编写了以下简单程序:
In order to play with unordered_map, I have written the following simple program:
#include <QtCore/QCoreApplication>
#include <QtCore>
#include <iostream>
#include <stdio.h>
#include <string>
#include <unordered_map>
using std::string;
using std::cout;
using std::endl;
typedef std::vector<float> floatVector;
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
floatVector c(10);
floatVector b(10);
for (int i = 0; i < 10; i++) {
c[i] = i + 1;
b[i] = i * 2;
}
std::unordered_map<floatVector, int> map;
map[b] = 135;
map[c] = 40;
map[c] = 32;
std::cout << "b -> " << map[b] << std::endl;
std::cout << "c -> " << map[c] << std::endl;
std::cout << "Contains? -> " << map.size() << std::endl;
return a.exec();
}
不幸的是,我遇到了以下没有启发性的错误.甚至连行号都没有.
Unfortunately, I am running into the folowing error which isn't inspiring. There is not even a line number.
:-1: 错误: collect2: ld 返回 1 个退出状态
:-1: error: collect2: ld returned 1 exit status
知道问题的根源吗?
推荐答案
§23.2.5,第 3 段,说:
§23.2.5, paragraph 3, says:
每个无序关联容器由 Key 参数化,通过满足 Hash 要求(17.6.3.4)的函数对象类型 Hash 并充当参数的哈希函数Key 类型的值,并通过一个二元谓词 Pred 诱导 Key 类型的值的等价关系.
Each unordered associative container is parameterized by
Key, by a function object typeHashthat meets the Hash requirements (17.6.3.4) and acts as a hash function for argument values of typeKey, and by a binary predicatePredthat induces an equivalence relation on values of typeKey.
使用 vector 作为 Key 并且不提供明确的哈希和等价谓词类型意味着默认的 std::hash 和 std::equal_to 将被使用.
Using vector<float> as Key and not providing explicit hash and equivalence predicate types means the default std::hash<vector<float>> and std::equal_to<vector<float>> will be used.
等价关系的std::equal_to很好,因为向量有一个运算符==,这就是std::equal_to 使用.
The std::equal_to for the equivalence relation is fine, because there is an operator == for vectors, and that's what std::equal_to uses.
然而,没有 std::hash<vector<float>> 特化,这可能就是你没有向我们展示的链接器错误所说的.您需要提供自己的哈希器才能使其正常工作.
There is however, no std::hash<vector<float>> specialization, and that's probably what the linker error you didn't show us says. You need to provide your own hasher for this to work.
编写这种散列器的一种简单方法是使用 boost::hash_range:
An easy way of writing such an hasher is to use boost::hash_range:
template <typename Container> // we can make this generic for any container [1]
struct container_hash {
std::size_t operator()(Container const& c) const {
return boost::hash_range(c.begin(), c.end());
}
};
然后你可以使用:
std::unordered_map<floatVector, int, container_hash<floaVector>> map;
当然,如果您需要在映射中使用不同的相等语义,则需要适当地定义散列和等价关系.
Of course, if you need different equality semantics in the map you need to define the hash and equivalence relation appropriately.
1.但是,在对无序容器进行散列时要避免这种情况,因为不同的顺序会产生不同的散列值,并且无法保证无序容器中的顺序.
这篇关于C++ unordered_map 在使用向量作为键时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C++ unordered_map 在使用向量作为键时失败
基础教程推荐
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 我有静态或动态 boost 库吗? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
