unordered_map hash函数2种写法

1 篇文章 0 订阅

```cpp
//#include "stdafx.h"
#include <iostream>
#include <cstdio>
#include <typeinfo>
#include <unordered_map>
#include <iterator>
#include <functional>
#include <algorithm>

using namespace std;


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

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

//可变参数模板函数
template<typename T, typename... Types>
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<typename... Types>
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<TestClass> {
		typedef size_t result_type;
		typedef TestClass argument_type;
		size_t operator()(const TestClass& lhs) const {
			//简单算法
			return hash<int>()(lhs.getVal()) ^ hash<char>()(lhs.getCh()) ^ hash<string>()(lhs.getStr());
		}
	};
}


//使用functor
struct testHash {
	size_t operator()(const TestClass& lhs) const {
		//简单算法
		return hash<int>()(lhs.getVal()) ^ hash<char>()(lhs.getCh()) ^ hash<string>()(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<TestClass, int> 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<TestClass, int, testHash> 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<TestClass, int, testHash2> 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;
}



  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值