jeudi 13 novembre 2014

C++: How to compute hash value from several hashable members?


Vote count:

0




Suppose I have a C++ class



class T {
Type1 member1;
Type2 member2;
Type3 member3;
unsigned long hash() {
// How to implement?
}
};


Assuming each member hash a hash function member.hash(). What is the best way to implement the hash function of class T? Java has a HashCodeBuilder class specific for this task, is there a counterpart in C++?


I know one possible solution could be something like



member1.hash() + member2.hash() * 17 + member3.hash() * 37


Is this generally a good hash function? And how should I choose the constants 17, 37, etc., esp. if I more than 3 members?


Another minor question is assuming one of my member is primitive type (int, float, string, etc.), how should I generate a hash value from it?


Thanks a lot!



asked 30 secs ago







C++: How to compute hash value from several hashable members?

Aucun commentaire:

Enregistrer un commentaire