I try to find a way to get the same result when I hash a given string in windows and in linux. but for example if I run the following code:
hash<string> h;
cout << h("hello");
it will return 3305111549 in windows and 2762169579135187400 in linux.
If it is not possible to get the same return value accross these 2 platforms, is there any other decent hash function that would return the same value on both systems?
No. As per std::hash reference, emphasis mine:
The actual hash functions are implementation-dependent and are not required to fulfill any other quality criteria except those specified above.
More specifically you are using the std::hash<std::string> template specialization whose hashes:
equal the hashes of corresponding std::basic_string_view classes
which are also implementation dependent. So no, you can not expect the same std::hash
results with different implementations. Furthermore since C++14:
Hash functions are only required to produce the same result for the same input within a single execution of a program;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With