<tfoot id='BTFJ7'></tfoot>
      <i id='BTFJ7'><tr id='BTFJ7'><dt id='BTFJ7'><q id='BTFJ7'><span id='BTFJ7'><b id='BTFJ7'><form id='BTFJ7'><ins id='BTFJ7'></ins><ul id='BTFJ7'></ul><sub id='BTFJ7'></sub></form><legend id='BTFJ7'></legend><bdo id='BTFJ7'><pre id='BTFJ7'><center id='BTFJ7'></center></pre></bdo></b><th id='BTFJ7'></th></span></q></dt></tr></i><div id='BTFJ7'><tfoot id='BTFJ7'></tfoot><dl id='BTFJ7'><fieldset id='BTFJ7'></fieldset></dl></div>

        <bdo id='BTFJ7'></bdo><ul id='BTFJ7'></ul>
    1. <small id='BTFJ7'></small><noframes id='BTFJ7'>

        <legend id='BTFJ7'><style id='BTFJ7'><dir id='BTFJ7'><q id='BTFJ7'></q></dir></style></legend>

        与 std::hash 意外冲突

        时间:2023-08-26

      1. <i id='St5xy'><tr id='St5xy'><dt id='St5xy'><q id='St5xy'><span id='St5xy'><b id='St5xy'><form id='St5xy'><ins id='St5xy'></ins><ul id='St5xy'></ul><sub id='St5xy'></sub></form><legend id='St5xy'></legend><bdo id='St5xy'><pre id='St5xy'><center id='St5xy'></center></pre></bdo></b><th id='St5xy'></th></span></q></dt></tr></i><div id='St5xy'><tfoot id='St5xy'></tfoot><dl id='St5xy'><fieldset id='St5xy'></fieldset></dl></div>

              <tfoot id='St5xy'></tfoot>

                  <tbody id='St5xy'></tbody>
                <legend id='St5xy'><style id='St5xy'><dir id='St5xy'><q id='St5xy'></q></dir></style></legend>
                  <bdo id='St5xy'></bdo><ul id='St5xy'></ul>

                  <small id='St5xy'></small><noframes id='St5xy'>

                  本文介绍了与 std::hash 意外冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我知道将无限数量的字符串散列到 32b int 必须产生冲突,但我希望散列函数有一些不错的分布.

                  I know hashing infinite number of string into 32b int must generate collision, but I expect from hashing function some nice distribution.

                  这两个字符串有相同的哈希值是不是很奇怪?

                  Isn't it weird that these 2 strings have the same hash?

                  size_t hash0 = std::hash<std::string>()("generated_id_0");
                  size_t hash1 = std::hash<std::string>()("generated_id_1");
                  //hash0 == hash1
                  

                  我知道我可以使用 boost::hash<std::string> 或其他,但我想知道 std::hash 有什么问题.我用错了吗?我不应该以某种方式播种"它吗?

                  I know I can use boost::hash<std::string> or others, but I want to know what is wrong with std::hash. Am I using it wrong? Shouldn't I somehow "seed" it?

                  推荐答案

                  您对 std::hash 的使用没有任何问题.问题在于,与 Visual Studio 2010 捆绑的标准库实现提供的专门化 std::hash<std::string> 仅采用字符串字符的子集来确定哈希值(大概用于性能原因).巧合的是,具有 14 个字符的字符串的最后一个字符不属于该集合,这就是为什么两个字符串产生相同哈希值的原因.

                  There's nothing wrong with your usage of std::hash. The problem is that the specialization std::hash<std::string> provided by the standard library implementation bundled with Visual Studio 2010 only takes a subset of the string's characters to determine the hash value (presumably for performance reasons). Coincidentally the last character of a string with 14 characters is not part of this set, which is why both strings yield the same hash value.

                  据我所知,这种行为符合标准,要求仅对具有相同参数的哈希函数的多次调用必须始终返回相同的值.然而,散列冲突的可能性应该是最小的.VS2010 实现实现了必选部分,但没有考虑可选部分.

                  As far as I know this behaviour is in conformance with the standard, which demands only that multiple calls to the hash function with the same argument must always return the same value. However, the probability of a hash collision should be minimal. The VS2010 implementation fulfills the mandatory part, yet fails to account for the optional one.

                  有关详细信息,请参阅头文件 xfunctional(从我的副本中的第 869 行开始)和 C++ 标准的 §17.6.3.4(最新公开草案).

                  For details, see the implementation in the header file xfunctional (starting at line 869 in my copy) and §17.6.3.4 of the C++ standard (latest public draft).

                  如果你绝对需要一个更好的字符串哈希函数,你应该自己实现它.实际上没那么难.

                  If you absolutely need a better hash function for strings, you should implement it yourself. It's actually not that hard.

                  这篇关于与 std::hash 意外冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 Qt 获取文件的 SHA-1/MD5 校验和? 下一篇:如何实现用于 std::unordered_map 的 CString 哈希函数?

                  相关文章

                  <i id='kDpKH'><tr id='kDpKH'><dt id='kDpKH'><q id='kDpKH'><span id='kDpKH'><b id='kDpKH'><form id='kDpKH'><ins id='kDpKH'></ins><ul id='kDpKH'></ul><sub id='kDpKH'></sub></form><legend id='kDpKH'></legend><bdo id='kDpKH'><pre id='kDpKH'><center id='kDpKH'></center></pre></bdo></b><th id='kDpKH'></th></span></q></dt></tr></i><div id='kDpKH'><tfoot id='kDpKH'></tfoot><dl id='kDpKH'><fieldset id='kDpKH'></fieldset></dl></div>
                    <tfoot id='kDpKH'></tfoot>

                      <bdo id='kDpKH'></bdo><ul id='kDpKH'></ul>

                    <small id='kDpKH'></small><noframes id='kDpKH'>

                    1. <legend id='kDpKH'><style id='kDpKH'><dir id='kDpKH'><q id='kDpKH'></q></dir></style></legend>