c++ - Good hash function for pair of primitive types -
i'm trying figure out hash function std::pair of 2 primitive types. way have implemented right now:
template<typename t, typename u> std::size_t operator()(const std::pair<t,u> &rhs) const { return stdext::hash_value<t>(rhs.first) ^ stdext::hash_value<u>(rhs.second); }
it appears work if have 2 pairs such (1, 2) , (2, 1) (numbers flipped). generate same hash value values still inserted hash map. thoughts?
generally speaking, hashing containers have handle case (hash collisions). there couple methods can use chaining , probing, of potentially hurt performance.
instead, suggest using boost::hash_combine
combine hashes in such way swapping first
, second
doesn't generate same hash.
Comments
Post a Comment