C++ unordered_map 在使用向量作为键时失败

C++ unordered_map fail when used with a vector as key(C++ unordered_map 在使用向量作为键时失败)
本文介绍了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 在使用向量作为键时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Bring window to front -gt; raise(),show(),activateWindow() don’t work(把窗户放在前面 -raise(),show(),activateWindow() 不起作用)
How to get a list video capture devices NAMES (web cameras) using Qt (crossplatform)? (C++)(如何使用 Qt(跨平台)获取列表视频捕获设备名称(网络摄像机)?(C++))
How to compile Qt as static(如何将 Qt 编译为静态)
C++ over Qt : Controlling transparency of Labels and Buttons(C++ over Qt:控制标签和按钮的透明度)
How to know when a new USB storage device is connected in Qt?(Qt如何知道新的USB存储设备何时连接?)
What is an event loop in Qt?(Qt 中的事件循环是什么?)