In order to use your own class as a key in unordered_map
(for example) you must define a hash function. Cppreference.com example
template<> struct hash<S> { typedef S argument_type; typedef std::size_t result_type; result_type operator()(argument_type const& s) const { result_type const h1 ( std::hash<std::string>()(s.first_name) ); result_type const h2 ( std::hash<std::string>()(s.last_name) ); return h1 ^ (h2 << 1); } };
this concerns two fields, I am confused by this moment h1 ^ (h2 << 1)
I saw examples without shift and now I don’t quite understand how I project this example on a larger number of fields, according to which rule. Can I just do h1 ^ h2 ^ ... ^ h(n)
? or do each subsequent hash I need to shift? h1 ^ ( h2 << 1 ) ^ ... ^ ( h(n) << n )
?
I would not like to encounter conflicts, they will be very difficult to detect, but they will spoil the work significantly.