如果我想在 Java 中为给定对象生成哈希,我知道的最简单的方法是使用 Apache Commons HashCodeBuilder
:
public class Person {
String name;
int age;
boolean smoker;
...
public int hashCode() {
// you pick a hard-coded, randomly chosen, non-zero, odd number
// ideally different for each class
return new HashCodeBuilder(17, 37).
append(name).
append(age).
append(smoker).
toHashCode();
}
}
C ++中有类似的东西吗?