C++: How to compute hash value from several hashable members? -
suppose have c++ class
class t { type1 member1; type2 member2; type3 member3; unsigned long hash() { // how implement? } };
assuming each member hash hash function member.hash()
. best way implement hash
function of class t
? java has hashcodebuilder
class specific task, there counterpart in c++?
i know 1 possible solution like
member1.hash() + member2.hash() * 17 + member3.hash() * 37
is hash function? , how should choose constants 17, 37, etc., esp. if more 3 members?
another minor question assuming 1 of member primitive type (int
, float
, string
, etc.), how should generate hash value it?
boost has this: hash_combine
size_t seed = 0; boost::hash_combine(seed, member1); boost::hash_combine(seed, member2); boost::hash_combine(seed, member3); return seed;
Comments
Post a Comment