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

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

      <i id='z2j5W'><tr id='z2j5W'><dt id='z2j5W'><q id='z2j5W'><span id='z2j5W'><b id='z2j5W'><form id='z2j5W'><ins id='z2j5W'></ins><ul id='z2j5W'></ul><sub id='z2j5W'></sub></form><legend id='z2j5W'></legend><bdo id='z2j5W'><pre id='z2j5W'><center id='z2j5W'></center></pre></bdo></b><th id='z2j5W'></th></span></q></dt></tr></i><div id='z2j5W'><tfoot id='z2j5W'></tfoot><dl id='z2j5W'><fieldset id='z2j5W'></fieldset></dl></div>
    2. <tfoot id='z2j5W'></tfoot>
        <bdo id='z2j5W'></bdo><ul id='z2j5W'></ul>
    3. C++ std::unordered_map 中使用的默认哈希函数是什么?

      What is the default hash function used in C++ std::unordered_map?(C++ std::unordered_map 中使用的默认哈希函数是什么?)
        <tbody id='MDkww'></tbody>

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

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

                <bdo id='MDkww'></bdo><ul id='MDkww'></ul>
                <i id='MDkww'><tr id='MDkww'><dt id='MDkww'><q id='MDkww'><span id='MDkww'><b id='MDkww'><form id='MDkww'><ins id='MDkww'></ins><ul id='MDkww'></ul><sub id='MDkww'></sub></form><legend id='MDkww'></legend><bdo id='MDkww'><pre id='MDkww'><center id='MDkww'></center></pre></bdo></b><th id='MDkww'></th></span></q></dt></tr></i><div id='MDkww'><tfoot id='MDkww'></tfoot><dl id='MDkww'><fieldset id='MDkww'></fieldset></dl></div>
              • 本文介绍了C++ std::unordered_map 中使用的默认哈希函数是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用

                unordered_map<string, int>
                

                unordered_map<int, int>
                

                每种情况下使用什么哈希函数,每种情况下发生碰撞的几率是多少?我将分别在每种情况下插入唯一字符串和唯一 int 作为键.

                What hash function is used in each case and what is chance of collision in each case? I will be inserting unique string and unique int as keys in each case respectively.

                我有兴趣了解字符串和 int 键的哈希函数算法及其碰撞统计.

                I am interested in knowing the algorithm of hash function in case of string and int keys and their collision stats.

                推荐答案

                函数对象 std::使用哈希<>.

                所有内置类型和一些其他标准库类型都存在标准特化例如 std::stringstd::thread.查看完整列表的链接.

                Standard specializations exist for all built-in types, and some other standard library types such as std::string and std::thread. See the link for the full list.

                对于要在 std::unordered_map 中使用的其他类型,您必须专门化 std::hash<> 或创建您自己的函数对象.

                For other types to be used in a std::unordered_map, you will have to specialize std::hash<> or create your own function object.

                冲突的可能性完全取决于实现,但考虑到整数限制在定义的范围内这一事实,而字符串理论上是无限长的,我认为与字符串发生冲突的可能性要大得多.

                The chance of collision is completely implementation-dependent, but considering the fact that integers are limited between a defined range, while strings are theoretically infinitely long, I'd say there is a much better chance for collision with strings.

                至于在 GCC 中的实现,内置类型的特化只返回位模式.以下是它们在 bits/functional_hash.h 中的定义:

                As for the implementation in GCC, the specialization for builtin-types just returns the bit pattern. Here's how they are defined in bits/functional_hash.h:

                  /// Partial specializations for pointer types.
                  template<typename _Tp>
                    struct hash<_Tp*> : public __hash_base<size_t, _Tp*>
                    {
                      size_t
                      operator()(_Tp* __p) const noexcept
                      { return reinterpret_cast<size_t>(__p); }
                    };
                
                  // Explicit specializations for integer types.
                #define _Cxx_hashtable_define_trivial_hash(_Tp)     
                  template<>                        
                    struct hash<_Tp> : public __hash_base<size_t, _Tp>  
                    {                                                   
                      size_t                                            
                      operator()(_Tp __val) const noexcept              
                      { return static_cast<size_t>(__val); }            
                    };
                
                  /// Explicit specialization for bool.
                  _Cxx_hashtable_define_trivial_hash(bool)
                
                  /// Explicit specialization for char.
                  _Cxx_hashtable_define_trivial_hash(char)
                
                  /// ...
                

                <小时>

                std::string 的特化定义为:

                #ifndef _GLIBCXX_COMPATIBILITY_CXX0X
                  /// std::hash specialization for string.
                  template<>
                    struct hash<string>
                    : public __hash_base<size_t, string>
                    {
                      size_t
                      operator()(const string& __s) const noexcept
                      { return std::_Hash_impl::hash(__s.data(), __s.length()); }
                    };
                

                一些进一步的搜索导致我们:

                Some further search leads us to:

                struct _Hash_impl
                {
                  static size_t
                  hash(const void* __ptr, size_t __clength,
                       size_t __seed = static_cast<size_t>(0xc70f6907UL))
                  { return _Hash_bytes(__ptr, __clength, __seed); }
                  ...
                };
                ...
                // Hash function implementation for the nontrivial specialization.
                // All of them are based on a primitive that hashes a pointer to a
                // byte array. The actual hash algorithm is not guaranteed to stay
                // the same from release to release -- it may be updated or tuned to
                // improve hash quality or speed.
                size_t
                _Hash_bytes(const void* __ptr, size_t __len, size_t __seed);
                

                _Hash_byteslibstdc++ 的外部函数.更多的搜索让我找到 这个文件,其中说明:

                _Hash_bytes is an external function from libstdc++. A bit more searching led me to this file, which states:

                // This file defines Hash_bytes, a primitive used for defining hash
                // functions. Based on public domain MurmurHashUnaligned2, by Austin
                // Appleby.  http://murmurhash.googlepages.com/
                

                所以 GCC 对字符串使用的默认散列算法是 MurmurHashUnaligned2.

                So the default hashing algorithm GCC uses for strings is MurmurHashUnaligned2.

                这篇关于C++ std::unordered_map 中使用的默认哈希函数是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What is inside .lib file of Static library, Statically linked dynamic library and dynamically linked dynamic library?(静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么?)
                How do I load a C DLL from the SXS in Python?(如何从 Python 中的 SXS 加载 C DLL?)
                Can Cython code be compiled to a dll so C++ application can call it?(Cython 代码可以编译成 dll 以便 C++ 应用程序可以调用它吗?)
                Delay Loading DLLs(延迟加载 DLL)
                Throwing C++ exceptions across DLL boundaries(跨 DLL 边界抛出 C++ 异常)
                Loading a dll from a dll?(从 dll 加载 dll?)

                  <tbody id='CedXR'></tbody>

                  <bdo id='CedXR'></bdo><ul id='CedXR'></ul>
                  <tfoot id='CedXR'></tfoot>

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

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

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