久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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 在使用向量作為鍵時失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 三级在线免费观看 | 一级高清免费毛片 | 中文字幕高清一区 | 在线免费av观看 | 九九久久国产 | 99精品在线 | 韩日在线视频 | 日日日操 | 中文字幕三区 | 在线综合视频 | 97人人干| 国产精品视频网站 | av香蕉 | 国产精品高清一区二区三区 | 欧美中文字幕一区二区三区亚洲 | 91精品国产综合久久久久久丝袜 | 久久午夜视频 | 日韩免费中文字幕 | 欧美一区二区三区在线观看 | 玖玖国产| 国产午夜在线 | 免费观看一级特黄欧美大片 | 国产免费视频 | 国产福利视频网站 | 啪一啪在线视频 | 日本成人片在线观看 | 在线观看视频一区 | 91一区| 综合久久久久 | 国产日韩一区二区三区 | 亚洲精品久久久久久久久久久 | 正在播放国产精品 | 国产成人久久精品 | 久久综合伊人一区二区三 | 一色桃子av一区二区 | 成在线人视频免费视频 | 久久国产传媒 | 国产成人精品一区二区三区四区 | 91精品国产91久久久久久不卞 | 亚洲精品电影在线 | 一区二区在线不卡 |