c++ hash<string> 有没有办法在 linux 和 windows 中获得相同的值

标签 c++ hash

当我在 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/

相关文章:

c++ - 如何调用带参数的类模板函数

c++ - 将字节数组转换为结构指针取决于字节序或内存对齐方式?

c++ - 带有 Boost 的 CMake 找不到静态库

c++ - 将 shared_ptr 传递给函数 - 需要说明

c++ - 如果存在哈希,加速 c++ unordered_map 插入?

perl - perl hash 可以有多少个元素?

perl - 在散列元素中使用未初始化的值 $_

c++ - `std::set` 有什么问题?

python - 优化: Search the best way to compare two list of dict (Python)

c++ - 在 C++ 中减慢 SHA-256?