c++ unordered_map/set自定義物件的hash

lipeng08發表於2017-01-11

平時很少用到unordered_set的自定義物件,常用的都是unordered_map<int>, unordered_map<string>之類的內建資料型別。前段時間在寫一個編碼庫的時候,用到了自定義物件,卻無從下手,在此對其進行總結。

unordered_map/set是採用hash雜湊進行儲存的,因此儲存的物件必須提供兩個方法,1,hash告知此容器如何生成hash的值,2,equal_to 告知容器當出現hash衝突的時候,如何區分hash值相同的不同物件

假定要儲存的物件的類名為Object,則具體有4種方案:
1,定義兩個函式物件ObjectHash,以及ObjectEqu,分別實現對Object進行hash,以及比較兩個物件是否相同
2,定義兩個普通的c型別的函式,實現hahs以及物件比較,與1不同的是,普通函式在構建unordered_map/set的時候,需要decltype來減少顯示宣告它的型別(當前可以手動指定型別,很長)
例如: std::function<size_t(const Object&)>說明hash型別,或者std::function<bool (const Object&, const Object&)>說明比較型別
3,定義兩個lambda表示式(仿函式),與2類似
4,對Object物件進行模板特化

具體程式碼如下:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <iomanip>

#include <cstring>
#include <cmath>
#include <cstdlib>
#include <cstdio>

using namespace std;

//改變這個啟用不同的hash方案
#define  RECORD_NAMESPACE

struct Record
{
    string name;
    int val;
};

#ifdef RECORD_FUNCTION_OBJECT
struct RecordHash
{
    size_t operator()(const Record& rhs) const{
        return hash<string>()(rhs.name) ^ hash<int>()(rhs.val);
    }
};
struct RecordCmp
{
    bool operator()(const Record& lhs, const Record& rhs) const{
        return lhs.name == rhs.name && lhs.val == rhs.val;
    }
};
unordered_set<Record, RecordHash, RecordCmp> records = {
    { "b", 100 }, { "a", 80 }, { "cc", 70 }, { "d", 60 }, { "d", 60 }
};
#endif//RECORD_FUNCTION_OBJECT

#ifdef RECORD_C_FUNCTION
size_t RecordHash(const Record& rhs){
    return hash<string>()(rhs.name) ^ hash<int>()(rhs.val);
}
bool RecordCmp(const Record& lhs, const Record& rhs){
    return lhs.name == rhs.name && lhs.val == rhs.val;
}
//直接使用成員初始化列表,vs2013不能編譯通過
unordered_set<Record, decltype(&RecordHash), decltype(&RecordCmp)> records = {
    10,
    RecordHash, RecordCmp
};
struct RunBeforeMain
{
    RunBeforeMain(){
        records.insert({ "a", 100 });
    }
};
static RunBeforeMain dummyObject;
#endif //RECORD_C_FUNCTION

#ifdef RECORD_LAMBDA
//直接使用auto RecordHash不能編譯通過,vs2013
auto &RecordHash = [](const Record& rhs){
    return hash<string>()(rhs.name) ^ hash<int>()(rhs.val);
};
auto &RecordCmp = [](const Record& lhs, const Record& rhs){
    return lhs.name == rhs.name && lhs.val == rhs.val;
};
unordered_set<Record, decltype(RecordHash), decltype(RecordCmp)> records = {
    10,
    RecordHash, RecordCmp
};
struct RunBeforeMain
{
    RunBeforeMain(){
        records.insert({ "a", 100 });
    }
};
static RunBeforeMain dummyObject;
#endif//RECORD_LAMBDA

#ifdef RECORD_NAMESPACE
namespace std{
    template<>
    struct hash<Record>
    {
        size_t operator()(const Record& rhs) const{
            return hash<string>()(rhs.name) ^ hash<int>()(rhs.val);
        }
    };

    template<>
    struct equal_to < Record > {
        bool operator()(const Record& lhs, const Record& rhs) const{
            return lhs.name == rhs.name && lhs.val == rhs.val;
        }
    };
}
unordered_set<Record> records = {
    { "b", 100 }, { "a", 80 }, { "cc", 70 }, { "d", 60 }, { "d", 60 }
};
#endif //RECORD_NAMESPACE


int main()
{
    auto showRecords = [](){
        for (auto i : records)
        {
            cout << "{" << i.name << "," << i.val << "}" << endl;
        }
    };
    showRecords();
    return 0;
}

相關文章