1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <unordered_map>
int main() {
// A custom hash function that could cause many collisions
struct BadHash {
std::size_t operator()(int x) const {
return x % 10; // Not a very good hash function
}
};
std::unordered_map<int, std::string, BadHash> myMap;
myMap[10] = "Ten";
myMap[20] = "Twenty"; // Likely to collide with 10
// Inserting and accessing elements
std::cout << "10: " << myMap[10] << std::endl;
std::cout << "20: " << myMap[20] << std::endl;
return 0;
}
Back to std_unordered_map