久久久久久久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.

為了玩轉(zhuǎn)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();
}

不幸的是,我遇到了以下沒有啟發(fā)性的錯誤.甚至連行號都沒有.

Unfortunately, I am running into the folowing error which isn't inspiring. There is not even a line number.

:-1: 錯誤: collect2: ld 返回 1 個退出狀態(tài)

:-1: error: collect2: ld returned 1 exit status

知道問題的根源嗎?

推薦答案

§23.2.5,第 3 段,說:

§23.2.5, paragraph 3, says:

每個無序關(guān)聯(lián)容器由 Key 參數(shù)化,通過滿足 Hash 要求(17.6.3.4)的函數(shù)對象類型 Hash 并充當參數(shù)的哈希函數(shù)Key 類型的值,并通過一個二元謂詞 Pred 誘導 Key 類型的值的等價關(guān)系.

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.

等價關(guān)系的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;

當然,如果您需要在映射中使用不同的相等語義,則需要適當?shù)囟x散列和等價關(guān)系.

Of course, if you need different equality semantics in the map you need to define the hash and equivalence relation appropriately.

1.但是,在對無序容器進行散列時要避免這種情況,因為不同的順序會產(chǎn)生不同的散列值,并且無法保證無序容器中的順序.

這篇關(guān)于C++ unordered_map 在使用向量作為鍵時失敗的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數(shù)據(jù)?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環(huán): for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環(huán)?)
Reusing thread in loop c++(在循環(huán) C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環(huán)形?)
主站蜘蛛池模板: 免费一区二区 | 97超碰人人草 | 国产精品成人一区二区三区夜夜夜 | 亚洲精品免费视频 | 精品久 | 亚洲三区在线 | 亚洲精品资源 | 激情亚洲 | 久久精品91久久久久久再现 | 亚洲一区 中文字幕 | 国产精品久久久久久久久久三级 | 中文字幕日韩在线观看 | 欧美日韩综合一区 | 在线观看中文字幕 | www.日韩在线 | 91久久久久 | 久久免费精品视频 | 成人性视频在线 | 国产一级精品毛片 | 国产三级一区二区 | 亚洲成人久久久 | 台湾佬久久 | 免费黄色a级毛片 | 99精品免费在线观看 | 欧美电影网 | 涩涩操 | 国产福利在线视频 | 久久久影院 | 日韩精品在线观看一区二区 | 国产免费一区二区 | 在线亚州 | 国产精品一区久久久 | 青春草在线 | 国产精品亚洲一区二区三区在线 | 欧美一级黄色片免费观看 | 免费毛片www com cn | 中文字幕国产在线 | 毛片在线免费 | 久久久久香蕉视频 | 欧美一区二区三区在线免费观看 | 日韩精品在线观看视频 |