C++ 整数对哈希函数的错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20590656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 23:13:41  来源:igfitidea点击:

error for hash function of pair of ints

c++c++11hashstlunordered-map

提问by Shambo

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

我有以下带有unordered_map成员的类,以及为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 ;
        }
    };
}

But I am getting the following errors :

但我收到以下错误:

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>'

回答by Casey

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

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

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.

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

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

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;
};

EDIT: 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.

编辑:我在这里使用 xor 来组合配对成员的散列,因为我很懒,但是对于认真使用xor 是一个相当糟糕的散列组合函数

回答by Vladimir Reshetnikov

I prefer to rely on the standard implementation of std::hash<uintmax_t>to mix hashes of components of an std::pair:

我更喜欢依靠 的标准实现std::hash<uintmax_t>来混合 的组件的散列std::pair

#include <functional>
#include <utility>

struct hash_pair final {
    template<class TFirst, class TSecond>
    size_t operator()(const std::pair<TFirst, TSecond>& p) const noexcept {
        uintmax_t hash = std::hash<TFirst>{}(p.first);
        hash <<= sizeof(uintmax_t) * 4;
        hash ^= std::hash<TSecond>{}(p.second);
        return std::hash<uintmax_t>{}(hash);
    }
};