Search code examples
c++unordered-set

c++ cannot find element is unordered_set with the same hash


I have custom hash function for unordered_set of vectors< int >:

struct VectorHash {
int operator()(const vector<int> &V) const {
    int hsh=V[0] + V[1];
    return  hash<int>()(hsh);
}};

And for two such vectors I have the same hash equal 3:

vector<int> v1{2,1};
vector<int> v2{1,2};

But when I try to insert first vector v1 in unordered_set, and then check if I have the same vector by hash as v2 in my unordered_set I get false:

std::unordered_set<std::vector<int>, VectorHash> mySet;
mySet.insert(v1); 

if(mySet.find(v2) == mySet.end())
    cout << "didn't find" << endl;

Output:  "didn't find"

I assume that if two elements in unordered_set have the same hash then if I have v1 in my unordered_set, find method should return true, when I try to find v2. But it is not the case.

Could anyone explain me what is wrong in my reasoning?


Solution

  • Hash isn't everything, what you're seeing here, is a collision.

    Both std::vector<int> have the same hash value here, but after hash is calculated, std::unordered_map will actually actually check for equality of elements using operator== to check for equality of elements, which fails in this case, and fails to find the element.

    Collisions are a normal thing in HashMaps, not much you can do here without providing custom operator==.