unordered_map hash函数2种写法


```cpp
//#include "stdafx.h"
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;


template
inline void hash_combine(size_t& seed, const T& val) {
	//哈希函数算法
	seed ^= std::hash()(val) + 0x9e3779b6 + (seed << 6) + (seed >> 2);
}

//递归边界
template
inline void hash_val(size_t& seed, const T& val) {
	hash_combine(seed, val);
	return;
}

//可变参数模板函数
template
inline void hash_val(size_t& seed, const T& val, const Types&...args) {
	hash_combine(seed, val); //逐一取val, 改变seed
	hash_val(seed, args...);
	return;
}

template
inline size_t hash_val(const Types&... args) {
	size_t seed = 0;
	hash_val(seed, args...);
	return seed;
}


class TestClass {
public:
	TestClass(const int i, const char c, const string s) : val(i), ch(c), str(s) {}
	bool operator==(const TestClass& other) const {
		return this->val == other.val && this->ch == other.ch && this->str == other.str;
	}

	int getVal() const {
		return this->val;
	}
	char getCh() const {
		return this->ch;
	}
	string getStr() const {
		return this->str;
	}

	friend ostream& operator<<(ostream& os, const TestClass& lhs) {
		os << lhs.val << " " << lhs.ch << " " << lhs.str << " ";
		return os;
	}

private:
	int val = 100;
	char ch = 'a';
	string str;
};

//模板特例化
namespace std {
	//形参非const
	template<> struct hash {
		typedef size_t result_type;
		typedef TestClass argument_type;
		size_t operator()(const TestClass& lhs) const {
			//简单算法
			return hash()(lhs.getVal()) ^ hash()(lhs.getCh()) ^ hash()(lhs.getStr());
		}
	};
}


//使用functor
struct testHash {
	size_t operator()(const TestClass& lhs) const {
		//简单算法
		return hash()(lhs.getVal()) ^ hash()(lhs.getCh()) ^ hash()(lhs.getStr());
	}
};

struct testHash2 {
	size_t operator()(const TestClass& lhs) const {
		//较合理算法
		return hash_val(lhs.getVal(), lhs.getCh(), lhs.getStr());
	}
};


int main(int argc, char *argv[])
{
	unordered_map test_count;
	test_count.insert({ TestClass(1, 'f', "me"), 100 });
	test_count.insert({ TestClass(2, 'g', "you"), 200 });
	test_count.insert({ TestClass(3, 'k', "him"), 300 });
	for (auto beg = test_count.begin(); beg != test_count.end(); ++beg) {
		cout << beg->first << " " << beg->second << endl;
	}
	cout << endl << endl;

	unordered_map test_count1;
	test_count1.insert({ TestClass(1, 'f', "me"), 100 });
	test_count1.insert({ TestClass(2, 'g', "you"), 200 });
	test_count1.insert({ TestClass(3, 'k', "him"), 300 });
	for (auto beg = test_count1.begin(); beg != test_count1.end(); ++beg) {
		cout << beg->first << " " << beg->second << endl;
	}
	cout << endl << endl;

	unordered_map test_count2;
	test_count2.insert({ TestClass(1, 'f', "me"), 100 });
	test_count2.insert({ TestClass(2, 'g', "you"), 200 });
	test_count2.insert({ TestClass(3, 'k', "him"), 300 });
	for (auto beg = test_count2.begin(); beg != test_count2.end(); ++beg) {
		cout << beg->first << " " << beg->second << endl;
	}
	cout << endl << endl;

	//两种算法哈希值比较
	cout << testHash()(TestClass(1, 'f', "me")) << endl;
	cout << testHash2()(TestClass(1, 'f', "me")) << endl;

	//system("pause");
	return 0;
}



你可能感兴趣的:(C++,hhash)