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?