1. 程式人生 > >C++11使用自定義hash函式及比較函式的unordered_set

C++11使用自定義hash函式及比較函式的unordered_set

#include <unordered_set>
#include <functional>
#include <iostream>

struct MyKey
{
	int key;
};

struct MyKeyHashHasher
{
	size_t operator()(const MyKey &k) const noexcept
	{
		return std::hash<int>{}(k.key);
	}
};

struct MyKeyHashComparator
{
	bool operator()(const
MyKey &k1, const MyKey &k2) const noexcept { return k1.key == k2.key; } }; int main() { std::unordered_set<MyKey,MyKeyHashHasher,MyKeyHashComparator> ss; return 0; }