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

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

      1. <tfoot id='rkaCY'></tfoot>

        整数对哈希函数的错误

        error for hash function of pair of ints(整数对哈希函数的错误)

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

              <legend id='m2ToA'><style id='m2ToA'><dir id='m2ToA'><q id='m2ToA'></q></dir></style></legend>
              <i id='m2ToA'><tr id='m2ToA'><dt id='m2ToA'><q id='m2ToA'><span id='m2ToA'><b id='m2ToA'><form id='m2ToA'><ins id='m2ToA'></ins><ul id='m2ToA'></ul><sub id='m2ToA'></sub></form><legend id='m2ToA'></legend><bdo id='m2ToA'><pre id='m2ToA'><center id='m2ToA'></center></pre></bdo></b><th id='m2ToA'></th></span></q></dt></tr></i><div id='m2ToA'><tfoot id='m2ToA'></tfoot><dl id='m2ToA'><fieldset id='m2ToA'></fieldset></dl></div>
                <tbody id='m2ToA'></tbody>
              <tfoot id='m2ToA'></tfoot>
              • <small id='m2ToA'></small><noframes id='m2ToA'>

                1. 本文介绍了整数对哈希函数的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有以下带有 unordered_map 成员的类,以及为 pair

                  I have the following class with an unordered_map member, and a hash function defined for pair<int,int>

                  class abc
                  {public :
                      unordered_map < pair<int,int> , int > rules ;
                      unsigned nodes;
                      unsigned packet ;     
                  };
                  
                  namespace std {
                  template <>
                      class hash < std::pair< int,int> >{
                      public :
                          size_t operator()(const pair< int, int> &x ) const
                          {
                              size_t h =   std::hash<int>()(x.first) ^ std::hash<int>()(x.second);
                              return  h ;
                          }
                      };
                  }
                  

                  但我收到以下错误:

                  error: invalid use of incomplete type ‘struct std::hash<std::pair<int, int> >
                  
                  error: declaration of ‘struct std::hash<std::pair<int, int> >
                  
                  error: type ‘std::__detail::_Hashtable_ebo_helper<1, std::hash<std::pair<int, int> >, true>’ is not a direct base of ‘std::__detail::_Hash_code_base<std::pair<int, int>, std::pair<const std::pair<int, int>, int>, std::__detail::_Select1st, std::hash<std::pair<int, int> >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, true>’
                  

                  推荐答案

                  不幸的是,这个程序有未定义的行为.C++11 §17.6.4.2.1:

                  Unfortunately, this program has undefined behavior. C++11 §17.6.4.2.1:

                  只有当声明依赖于用户定义的类型并且特化满足原始模板的标准库要求并且未被明确禁止时,程序才可以将任何标准库模板的模板特化添加到命名空间 std.

                  A program may add a template specialization for any standard library template to namespace std only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.

                  hash> 仅取决于原始和标准库类型.通过在名称空间 std 之外定义散列类,并在映射声明中显式使用该散列,可以轻松解决此问题:

                  hash<pair<int,int>> depends on primitive and standard library types only. This is easily worked around by defining your hash class outside of namespace std, and using that hash explicitly in your map declaration:

                  struct pairhash {
                  public:
                    template <typename T, typename U>
                    std::size_t operator()(const std::pair<T, U> &x) const
                    {
                      return std::hash<T>()(x.first) ^ std::hash<U>()(x.second);
                    }
                  };
                  
                  class abc {
                    std::unordered_map<std::pair<int,int>, int, pairhash> rules;
                  };
                  

                  我在这里使用 xor 来组合配对成员的哈希值,因为我很懒,但要认真使用 xor 是一个相当糟糕的哈希组合函数.

                  I've used xor to combine the hashes of the pair members here because I'm lazy, but for serious use xor is a fairly crappy hash combining function.

                  这篇关于整数对哈希函数的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)

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

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

                      • <tfoot id='FOG8t'></tfoot>

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