In the C++ language there is the default hash-function template std::hash<T>
for the most simple types, like std::string
, int
, etc. I suppose, that these functions have a good entropy and the corresponding random variable distribution is statistically uniform. If it's not, then let's pretend, that it is.
Then, I have a structure:
struct CustomType {
int field1;
short field2;
string field3;
// ...
};
I want to hash it, using separate hashes of some of it's fields, say, std::hash(field1)
and std::hash(field2)
. Both hashes are in a set of possible values of the type size_t
.
What is a good hash-function, that can combine both those results and map them back to size_t
?
boost::hash_combine
is really good one to hash different fields.If you don't have boost library you can use this :
And then probably
std::unordered_set<S> s;
, etcboost::hash_combine
is something that could help you out here:See the boost doc here.