C++ unordered_map 在使用向量作为键时失败 [英] C++ unordered_map fail when used with a vector as key

查看:141
本文介绍了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 type Hash that meets the Hash requirements (17.6.3.4) and acts as a hash function for argument values of type Key, and by a binary predicate Pred that induces an equivalence relation on values of type Key.

使用 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 在使用向量作为键时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆