-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmissing-hash.cpp
58 lines (50 loc) · 1.55 KB
/
missing-hash.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
54
55
56
57
58
/*
The problem is that you are attempting to use `std::unordered_set`
with `std::pair<int, int>` as the key type. However, the standard
library does not provide a hash function specialization for
`std::pair` out of the box, so the default constructor of the
unordered set is deleted.
To resolve this, you'll need to provide a custom hash function for
`std::pair<int, int>`. Here's an example of how you can define one:
```cpp
struct PairHash {
template <typename T1, typename T2>
std::size_t operator()(const std::pair<T1, T2>& pair) const {
std::hash<T1> hash1;
std::hash<T2> hash2;
return hash1(pair.first) ^ (hash2(pair.second) << 1);
}
};
```
Then, when instantiating the `std::unordered_set`, you can specify the
custom hash function:
```cpp
std::unordered_set<std::pair<int, int>, PairHash> visited;
```
With this change, the code should now compile and work as expected.
*/
#include <functional>
#include <queue>
#include <unordered_set>
#include <utility>
#include <vector>
struct Node {
const std::pair<int, int> position;
std::vector<Node*> neighbors;
};
void bfs(Node* start, std::function<void(Node*)> f) {
std::unordered_set<std::pair<int, int>> visited;
std::queue<Node*> queue;
queue.push(start);
while (!queue.empty()) {
auto* n = queue.front();
queue.pop();
const auto [_, inserted] = visited.emplace(n->position);
if (inserted) {
f(n);
for (auto* neighbor : n->neighbors) {
queue.push(neighbor);
}
}
}
}