1. boost::unorder_map 实现自定义KEY
1 // boostLibTest.cpp : 定义控制台应用程序的入口点。 2 // 3 #include "stdafx.h" 4 5 #include <boost/functional/hash.hpp> 6 #include <boost/unordered_map.hpp> 7 #include <iostream> 8 #include <set> 9 #include <map> 10 #include <unordered_map> 11 12 using namespace std; 13 14 struct Test 15 { 16 int _id; 17 string _name; 18 set<int> _nums; 19 20 Test(int id, string name, set<int> nums = set<int>()) : 21 _id(id), _name(name), _nums(nums) 22 { 23 } 24 }; 25 26 bool operator==(const Test& ts1, const Test& ts2) 27 { 28 return ts1._id == ts2._id && ts1._name == ts2._name && ts1._nums == ts2._nums; 29 } 30 31 size_t hash_value(const Test& test) 32 { 33 std::size_t seed = 0; 34 boost::hash_combine(seed, std::hash_value(test._id)); 35 boost::hash_combine(seed, std::hash_value(test._name)); 36 for (auto& iter : test._nums){ 37 boost::hash_combine(seed, std::hash_value(iter)); 38 } 39 return seed; 40 } 41 42 int _tmain(int argc, _TCHAR* argv[]) 43 { 44 set<int> sets = {1,2,3,4,5}; 45 boost::unordered_map<Test, string> map; 46 map.insert(make_pair(Test(1, "abc", sets), "123")); 47 map.insert(make_pair(Test(2, "def", sets), "234")); 48 map.insert(make_pair(Test(3, "egh", sets), "345")); 49 map.insert(make_pair(Test(4, "ijk", sets), "456")); 50 map.insert(make_pair(Test(5, "lmn", sets), "567")); 51 52 auto iter = map.find(Test(3, "egh", sets)); 53 if (iter != map.end()){ 54 cout << "Find !" << endl; 55 } 56 else{ 57 cout << "Not Find !" << endl; 58 } 59 60 getchar(); 61 return 0; 62 }
输出结果为: Find !
===============================================================
2. std::unorder_map 实现自定义KEY
1 // boostLibTest.cpp : 定义控制台应用程序的入口点。 2 // 3 #include "stdafx.h" 4 5 #include <boost/functional/hash.hpp> 6 #include <boost/unordered_map.hpp> 7 #include <iostream> 8 #include <set> 9 #include <map> 10 #include <unordered_map> 11 12 using namespace std; 13 14 struct Test 15 { 16 int _id; 17 string _name; 18 set<int> _nums; 19 20 Test(int id, string name, set<int> nums = set<int>()) : 21 _id(id), _name(name), _nums(nums) 22 { 23 } 24 }; 25 26 namespace std 27 { 28 template<> 29 struct hash<Test> 30 : public _Bitwise_hash < Test > 31 { // hash functor for RECT 32 }; 33 34 inline bool operator == (const Test &ts1, const Test &ts2) _NOEXCEPT 35 { 36 return ts1._id == ts2._id && ts1._name == ts2._name && ts1._nums == ts2._nums; 37 } 38 } 39 40 int _tmain(int argc, _TCHAR* argv[]) 41 { 42 set<int> sets = {1,2,3,4,5}; 43 std::unordered_map<Test, string> map; 44 map.insert(make_pair(Test(1, "abc", sets), "123")); 45 map.insert(make_pair(Test(2, "def", sets), "234")); 46 map.insert(make_pair(Test(3, "egh", sets), "345")); 47 map.insert(make_pair(Test(4, "ijk", sets), "456")); 48 map.insert(make_pair(Test(5, "lmn", sets), "567")); 49 50 auto iter = map.find(Test(3, "egh", sets)); 51 if (iter != map.end()){ 52 cout << "Find !" << endl; 53 } 54 else{ 55 cout << "Not Find !" << endl; 56 } 57 58 getchar(); 59 return 0; 60 }
奇怪的地方来了,std的结果有时候是 Find ! 有时候是 Not Find !
没弄明白这个std::unorder_map怎么回事,我自己直接用了boost::unorder_map