-
Notifications
You must be signed in to change notification settings - Fork 160
/
unordered_set.cpp
53 lines (41 loc) · 1.59 KB
/
unordered_set.cpp
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
- likely hash map backed, thus O(1) instead of O(log(n)) for set
- uses the std::hash function
http://stackoverflow.com/questions/15869066/inserting-into-unordered-set-with-custom-hash-function
http://en.cppreference.com/w/cpp/utility/hash
- stdlib does not have a hash for tuples... so combining hashes of instance variables is the main question then.
So you just go with `boost::hash_combine` for now...
- http://codereview.stackexchange.com/questions/136770/hashing-a-tuple-in-c17
- http://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
- http://stackoverflow.com/questions/35985960/c-why-is-boosthash-combine-the-best-way-to-combine-hash-values
- http://stackoverflow.com/questions/3611951/building-an-unordered-map-with-tuples-as-keys
- http://stackoverflow.com/questions/7110301/generic-hash-for-tuples-in-unordered-map-unordered-set
- a custom hash for a class can be given either via constructor, or by embedding into std::
*/
#include "common.hpp"
class MyClass {
public:
int i;
int j;
MyClass(int i, int j) : i(i), j(j) {}
};
bool operator==(const MyClass& lhs, const MyClass& rhs) {
return lhs.i == rhs.i;
}
namespace std {
template <>
struct hash<MyClass> {
std::size_t operator()(const MyClass& k) const {
return k.i;
}
};
}
int main() {
std::unordered_set<MyClass> mySet;
assert(mySet.size() == 0);
mySet.insert(MyClass(1, 2));
assert(mySet.size() == 1);
mySet.insert(MyClass(1, 3));
assert(mySet.size() == 1);
assert(mySet.find(MyClass(1, -1))->j == 2);
}