当我在 windows 和 linux 中散列给定的字符串时,我试图找到一种方法来获得相同的结果。 但例如,如果我运行以下代码:
hash<string> h;
cout << h("hello");
它将在 windows 中返回 3305111549,在 linux 中返回 2762169579135187400。
如果不可能在这两个平台上获得相同的返回值,是否有任何其他体面的哈希函数可以在两个系统上返回相同的值?
最佳答案
没有。根据 std::hash引用,强调我的:
The actual hash functions are implementation-dependent and are not required to fulfill any other quality criteria except those specified above.
更具体地说,您正在使用 std::hash<std::string> 其散列的模板特化:
equal the hashes of corresponding std::basic_string_view classes
这也是依赖于实现的。所以不,你不能期望不同的实现产生相同的 std::hash
结果。此外,自 C++14 以来:
Hash functions are only required to produce the same result for the same input within a single execution of a program;
关于c++ hash<string> 有没有办法在 linux 和 windows 中获得相同的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47203934/