example3_collisions.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <unordered_set>

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_set<int, BadHash> mySet = {10, 20, 30, 40, 50};

    mySet.insert(11);  // Likely to collide with 10

    std::cout << "Elements in mySet: ";
    for (int elem : mySet) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}
Back to std_unordered_set