gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 15:38:36 26 4
gpt4 key购买 nike

如果我使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com