<bdo id='7Gv1q'></bdo><ul id='7Gv1q'></ul>
  • <small id='7Gv1q'></small><noframes id='7Gv1q'>

    <legend id='7Gv1q'><style id='7Gv1q'><dir id='7Gv1q'><q id='7Gv1q'></q></dir></style></legend>

        <i id='7Gv1q'><tr id='7Gv1q'><dt id='7Gv1q'><q id='7Gv1q'><span id='7Gv1q'><b id='7Gv1q'><form id='7Gv1q'><ins id='7Gv1q'></ins><ul id='7Gv1q'></ul><sub id='7Gv1q'></sub></form><legend id='7Gv1q'></legend><bdo id='7Gv1q'><pre id='7Gv1q'><center id='7Gv1q'></center></pre></bdo></b><th id='7Gv1q'></th></span></q></dt></tr></i><div id='7Gv1q'><tfoot id='7Gv1q'></tfoot><dl id='7Gv1q'><fieldset id='7Gv1q'></fieldset></dl></div>
      1. <tfoot id='7Gv1q'></tfoot>
      2. 自定义类的 unordered_set 是否有默认哈希函数?

        Is there a default hash function for an unordered_set of a custom class?(自定义类的 unordered_set 是否有默认哈希函数?)

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

            <tbody id='tQ22a'></tbody>

        • <small id='tQ22a'></small><noframes id='tQ22a'>

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

                  问题描述

                  我第一次使用 std::unordered_set 并且有一个关于哈希函数的问题.据我了解,如果您不指定哈希函数,它将默认为 std::hash.

                  I'm using a std::unordered_set for the first time and have a question about the hash function. As far as I understand, if you don't specify a hash function it will default to std::hash<Key>.

                  我的一个班级中有一个 mySet 成员:

                  I have a mySet member in one of my classes:

                  typedef std::unordered_set<MyClass> USetType;
                  USetType mySet;
                  

                  当我尝试构建时,出现以下错误:

                  When I try to build, I get the following error:

                  错误 C2440:类型转换":无法从const MyClass"转换为size_t"

                  error C2440: 'type cast' : cannot convert from 'const MyClass' to 'size_t'

                  如果要将unordered_set 与自定义类一起使用,是否需要定义转换函数(到size_t)?有什么办法可以避免编写自己的哈希函数而只使用默认值?

                  Is it necessary to define a conversion function (to size_t) if you want to use unordered_set with a custom class? Is there any way to avoid writing your own hash function and just using the default?

                  推荐答案

                  如果不指定自己的哈希函子作为模板参数,它将默认为 std::hash,除非你定义它,否则它不存在.

                  If you don't specify your own hash functor as template argument, it will default to std::hash<MyClass>, which does not exist unless you define it.

                  最好在名称空间 std 中定义自己的 std::hash 特化:

                  Best define your own specialization of std::hash inside namespace std:

                  namespace std {
                    template <>
                    struct hash<MyClass>
                    {
                      typedef MyClass      argument_type;
                      typedef std::size_t  result_type;
                  
                      result_type operator()(const MyClass & t) const
                      {
                         /* ..calculate hash value for t */
                      }
                    };
                  }
                  

                  并确保在声明散列之前包含此代码.通过这种方式,您可以简单地将哈希声明为 std::unordered_set<MyClass>,而无需进一步的模板参数.

                  And make sure you include this code before the declaration of your hash. This way you can declare the hash simply as std::unordered_set<MyClass> with no need for further template arguments.

                  您没有指定 MyClass 在里面是什么样子,但典型的情况是您的用户定义类型只是由几个简单类型成员组成,对于这些成员,存在默认的散列函数.在这种情况下,您可能希望将各个类型的散列值组合为整个组合的散列值.Boost 库为此提供了一个名为 hash_combine 的函数.当然,不能保证它在您的特定情况下能很好地工作(这取决于数据值的分布和冲突的可能性),但它提供了一个良好且易于使用的起点.

                  You didn't specify what MyClass looks like inside, but a typical situation is that your user-defined type simply consists of several simple-type members, for which a default hash function exists. In this case, you will probably want to combine the hash values for the individual types to a hash value for the entire combination. The Boost library provides a function called hash_combine for this purpose. Of course, there is no guarantee that it will work well in your particular case (it depends on the distribution of data values and the likelihood of collisions), but it provides a good and easy-to-use starting point.

                  这里是一个如何使用它的例子,假设 MyClass 由两个字符串成员组成:

                  Here is an example of how to use it, assuming MyClass consists of two string members:

                  #include <unordered_set>
                  #include <boost/functional/hash.hpp>
                  
                  struct MyClass
                  {
                    std::string _s1;
                    std::string _s2;
                  };
                  
                  namespace std {
                    template <>
                    struct hash<MyClass>
                    {
                      typedef MyClass      argument_type;
                      typedef std::size_t  result_type;
                  
                      result_type operator()(const MyClass & t) const
                      {
                        std::size_t val { 0 };
                        boost::hash_combine(val,t._s1);
                        boost::hash_combine(val,t._s2);
                        return val;
                      }
                    };
                  }
                  
                  int main()
                  {
                    std::unordered_set<MyClass> s;
                    /* ... */
                    return 0;
                  }
                  

                  这篇关于自定义类的 unordered_set 是否有默认哈希函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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?)
                  <legend id='ah012'><style id='ah012'><dir id='ah012'><q id='ah012'></q></dir></style></legend>

                  • <bdo id='ah012'></bdo><ul id='ah012'></ul>
                      <tbody id='ah012'></tbody>

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

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