Posts C++11 使用自定义 hash 函数及比较函数的 unordered_set
Post
Cancel

C++11 使用自定义 hash 函数及比较函数的 unordered_set

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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;
}
This post is licensed under CC BY 4.0 by the author.