c++ - std::hash 是否保证在 stdlib 发行版中相同

标签 c++ c++11 hash stdhash

如果我使用 libstdc++ 做了 std::hash 然后在即将到来的 C++11 VS 2012 库上做了一个 - 他们会匹配吗?

我假设哈希实现不是 C++ 规范的一部分,并且会因分布而异?

最佳答案

标准只说明了这一点:

20.8.12 Class template hash The unordered associative containers defined in 23.5 use specializations of the class template hash as the default hash function. For all object types Key for which there exists a specialization hash, theinstantiation hash shall:

  • satisfy the Hash requirements (17.6.3.4), with Key as the function call argument type, the DefaultConstructible requirements (Table 19), the CopyAssignable requirements (Table 23),
  • be swappable (17.6.3.2) for lvalues,
  • provide two nested types result_type and argument_type which shall be synonyms for size_t and Key, respectively,
  • satisfy the requirement that if k1 == k2 is true, h(k1) == h(k2) is also true, where h is an object of type hash and k1 and k2 are objects of type Key.

在 17.6.3.4 中,这是最重要的(表 26):

Shall not throw exceptions. The value returned shall depend only on the argument k. [ Note: Thus all evaluations of the expression h(k) with the same value for k yield the same result. — end note ] [ Note: For two different values t1 and t2, the probability that h(t1) and h(t2) compare equal should be very small, approaching 1.0 / numeric_- limits::max(). — end note ]

所以一般来说,不,计算本身没有定义,结果不需要在实现上保持一致。就此而言,即使是同一个库的两个不同版本也可能给出不同的结果。

关于c++ - std::hash 是否保证在 stdlib 发行版中相同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11990794/

相关文章:

c++ - 将静态 const 成员重新声明为 constexpr 是否会自动将其限定为内联?

c++ - Windows 触发了 c 中的断点?

c++ - 错误 C2280 : attempting to reference a deleted function while declaring a C++ struct

python - 在 Python 中散列一个整数以匹配 Oracle 的 STANDARD_HASH

html - 最佳 SRI 哈希大小是多少?

c++ - 回调中的仿函数 : compilation errors

c++警告: enumeration value not handled in switch [-Wswitch]

c++ - std::unique_lock::try_lock_until() 是否忙等待?

c++ - 如何实现编译时字符串转换函数?

perl - 在 Perl 中预期的偶数列表中找到引用 - 可能的传递引用错误?