使用std :: pair作为键来创建std :: unordered_map [英] Creating an std::unordered_map with an std::pair as key

查看:3587
本文介绍了使用std :: pair作为键来创建std :: unordered_map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个std :: unordered_map与std ::对作为键。你可以想象,这将要求我明确地提供一个类来生成一个给定的键的哈希,以及一个键的相等比较器。这里是我的代码到目前为止:

  #include< unordered_map> 
#include< memory>
#include< utility>

template< class T,typename U>
struct PairHash {
size_t operator()(const std :: pair< T,U>& key){
return std :: hash& ^ std :: hash< U>()(key.second);
}
};

template< class T,typename U>
struct PairEqual {
bool operator()(const std :: pair< T,U>& lhs,const std :: pair< T,U>& rhs)const {
return lhs.first == rhs.first&& lhs.second == rhs.second;
}
};

struct GraphEdge {

};


int main(){

std :: unordered_map< std :: pair< int,int>,
std :: unique_ptr< GraphEdge> ;,
PairHash< int,int>,
PairEqual< int,int>边缘;

}

但是,这给了我一个最小)inscrutable编译器错误:

 在/usr/include/c++/5/bits/hashtable.h:35 :0,
来自/ usr / include / c ++ / 5 / unordered_map:47,
来自prog.cpp:1:
/usr/include/c++/5/bits/hashtable_policy.h :在实例化'struct std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> >':
/ usr / include / c ++ / 5 / type_traits:137:12:需要从'struct std :: __ and_< std :: __ is_fast_hash< PairHash< int,int& >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > >'
/ usr / include / c ++ / 5 / type_traits:148:38:需要从'struct std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int& >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
/usr/include/c++/5/bits/unordered_map.h:100:66:需要从'std :: unordered_map< std :: pair< int,int>,std :: unique_ptr< GraphEdge>,PairHash< int,int>,PairEqual< int,int> >'
prog.cpp:29:43:从这里需要
/usr/include/c++/5/bits/hashtable_policy.h:85:34:错误:没有匹配到调用' const()const Hash< int,int>)(const std :: pair< int,int>&)'
noexcept(declat< const _Hash&> >
^
prog.cpp:7:12:note注意:候选者:size_t PairHash< T,U> :: operator()(const std :: pair< _T1,_T2& = int; U = int; size_t = unsigned int]< near match>
size_t operator()(const std :: pair< T,U& key){
^
prog.cpp:7:12:note:passing'const PairHash& int> *'as'this'argument放弃限定符
在/usr/include/c++/5/bits/move.h:57:0包含的文件中,
来自/ usr / include / c ++ / 5 / bits / stl_pair.h:59,
来自/ usr / include / c ++ / 5 / utility:70,
来自/ usr / include / c ++ / 5 / unordered_map:38,
从prog.cpp:1:
/ usr / include / c ++ / 5 / type_traits:在实例化'struct std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int& >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >':
/usr/include/c++/5/bits/unordered_map.h:100:66:需要从'std :: unordered_map< std :: pair< int,int>,std :: unique_ptr< ; GraphEdge>,PairHash< int,int>,PairEqual< int,int> >'
prog.cpp:29:43:required from here
/ usr / include / c ++ / 5 / type_traits:148:38:error:'value'不是'std: :__ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > >'
:public integral_constant< bool,!_Pp :: value>
^
在/ usr / include / c ++ / 5 / unordered_map:48:0,
从prog.cpp:1:
包含的文件/ usr / include / c ++ / 5 / bits / unordered_map.h:在实例化'class std :: unordered_map< std :: pair< int,int>,std :: unique_ptr< GraphEdge> ;, PairHash< int,int>,PairEqual< int,int& >':
prog.cpp:29:43:从这里需要
/usr/include/c++/5/bits/unordered_map.h:100:66:error:'value'不是'std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef __umap_hashtable< _Key,_Tp,_Hash,_Pred,_Alloc> _Hashtable;
^
/usr/include/c++/5/bits/unordered_map.h:107:45:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: key_type key_type;
^
/usr/include/c++/5/bits/unordered_map.h:108:47:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: value_type value_type;
^
/usr/include/c++/5/bits/unordered_map.h:109:48:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: mapped_type mapped_type;
^
/usr/include/c++/5/bits/unordered_map.h:110:43:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: hasher hasher;
^
/usr/include/c++/5/bits/unordered_map.h:111:46:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: key_equal key_equal;
^
/usr/include/c++/5/bits/unordered_map.h:112:51:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: allocator_type allocator_type;
^
/usr/include/c++/5/bits/unordered_map.h:117:45:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: pointer pointer;
^
/usr/include/c++/5/bits/unordered_map.h:118:50:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: const_pointer const_pointer;
^
/usr/include/c++/5/bits/unordered_map.h:119:47:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: reference reference;
^
/usr/include/c++/5/bits/unordered_map.h:120:52:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: const_reference const_reference;
^
/usr/include/c++/5/bits/unordered_map.h:121:46:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: iterator iterator;
^
/usr/include/c++/5/bits/unordered_map.h:122:51:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: const_iterator const_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:123:51:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: local_iterator local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:124:57:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: const_local_iterator const_local_iterator;
^
/usr/include/c++/5/bits/unordered_map.h:125:47:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: size_type size_type;
^
/usr/include/c++/5/bits/unordered_map.h:126:52:error:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
typedef typename _Hashtable :: difference_type difference_type;
^
/usr/include/c++/5/bits/unordered_map.h:280:7:错误:'value'不是'std :: __ not_< std :: __ and_< std的成员:: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
operator =(initializer_list< value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:379:2:error:'value'is不是'std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
emplace(_Args& ... __args)
^
/usr/include/c++/5/bits/unordered_map.h:432:7:error:'value '不是'std :: __ not_< std :: __和_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
insert(const value_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:439:2:error:'value'不是'std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
insert(_Pair&& __x)
^
/usr/include/c++/5/bits/unordered_map.h:499:7:错误:'value'不是'std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
insert(initializer_list< value_type> __l)
^
/usr/include/c++/5/bits/unordered_map.h:645:7:错误:'value'不是'std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
equal_range(const key_type& __x)
^
/usr/include/c++/5/bits/unordered_map.h:649:7:错误:'value'不是'std :: __ not_< std :: __ and_< std :: __ is_fast_hash< PairHash< int,int> >,std :: __ detail :: __ is_noexcept_hash< std :: pair< int,int>,PairHash< int,int> > > >'
equal_range(const key_type& __x)const
^

显然,libstdc ++通过 const PairHash< int指向你的哈希对象, int> * 。因此,调用程序中未标记为 const operator()是编译器错误。



您可以使用libstdc ++编译 operator() const



从Hash要求开始,Hash类型必须提供一个 size_t运算符(KeyType)const; ,因此您的代码确实不正确。


I am trying to create an std::unordered_map with an std::pair as key. As you can imagine, this would require me to explicitly provide a class to generate a hash for a given key, as well as an equality comparator for the keys. Here's my code so far:

#include <unordered_map>
#include <memory>
#include <utility>

template <class T, typename U> 
struct PairHash{ 
    size_t operator()(const std::pair<T, U> &key){ 
        return std::hash<T>()(key.first) ^ std::hash<U>()(key.second);
    }
};

template <class T, typename U>
struct PairEqual{
    bool operator()(const std::pair<T, U> &lhs, const std::pair<T, U> &rhs) const{
        return lhs.first == rhs.first && lhs.second == rhs.second;
    }
};

struct GraphEdge{

};


int main(){

    std::unordered_map<std::pair<int, int>, 
                       std::unique_ptr<GraphEdge>, 
                       PairHash<int, int>,
                       PairEqual<int, int>>  edges; 

}

However, this gives me a rather (to my eyes at least) inscrutable compiler error:

In file included from /usr/include/c++/5/bits/hashtable.h:35:0,
                 from /usr/include/c++/5/unordered_map:47,
                 from prog.cpp:1:
/usr/include/c++/5/bits/hashtable_policy.h: In instantiation of 'struct std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> >':
/usr/include/c++/5/type_traits:137:12:   required from 'struct std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > >'
/usr/include/c++/5/type_traits:148:38:   required from 'struct std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
/usr/include/c++/5/bits/unordered_map.h:100:66:   required from 'class std::unordered_map<std::pair<int, int>, std::unique_ptr<GraphEdge>, PairHash<int, int>, PairEqual<int, int> >'
prog.cpp:29:43:   required from here
/usr/include/c++/5/bits/hashtable_policy.h:85:34: error: no match for call to '(const PairHash<int, int>) (const std::pair<int, int>&)'
  noexcept(declval<const _Hash&>()(declval<const _Key&>()))>
                                  ^
prog.cpp:7:12: note: candidate: size_t PairHash<T, U>::operator()(const std::pair<_T1, _T2>&) [with T = int; U = int; size_t = unsigned int] <near match>
     size_t operator()(const std::pair<T, U> &key){ 
            ^
prog.cpp:7:12: note:   passing 'const PairHash<int, int>*' as 'this' argument discards qualifiers
In file included from /usr/include/c++/5/bits/move.h:57:0,
                 from /usr/include/c++/5/bits/stl_pair.h:59,
                 from /usr/include/c++/5/utility:70,
                 from /usr/include/c++/5/unordered_map:38,
                 from prog.cpp:1:
/usr/include/c++/5/type_traits: In instantiation of 'struct std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >':
/usr/include/c++/5/bits/unordered_map.h:100:66:   required from 'class std::unordered_map<std::pair<int, int>, std::unique_ptr<GraphEdge>, PairHash<int, int>, PairEqual<int, int> >'
prog.cpp:29:43:   required from here
/usr/include/c++/5/type_traits:148:38: error: 'value' is not a member of 'std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > >'
     : public integral_constant<bool, !_Pp::value>
                                      ^
In file included from /usr/include/c++/5/unordered_map:48:0,
                 from prog.cpp:1:
/usr/include/c++/5/bits/unordered_map.h: In instantiation of 'class std::unordered_map<std::pair<int, int>, std::unique_ptr<GraphEdge>, PairHash<int, int>, PairEqual<int, int> >':
prog.cpp:29:43:   required from here
/usr/include/c++/5/bits/unordered_map.h:100:66: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef __umap_hashtable<_Key, _Tp, _Hash, _Pred, _Alloc>  _Hashtable;
                                                                  ^
/usr/include/c++/5/bits/unordered_map.h:107:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::key_type key_type;
                                             ^
/usr/include/c++/5/bits/unordered_map.h:108:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::value_type value_type;
                                               ^
/usr/include/c++/5/bits/unordered_map.h:109:48: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::mapped_type mapped_type;
                                                ^
/usr/include/c++/5/bits/unordered_map.h:110:43: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::hasher hasher;
                                           ^
/usr/include/c++/5/bits/unordered_map.h:111:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::key_equal key_equal;
                                              ^
/usr/include/c++/5/bits/unordered_map.h:112:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::allocator_type allocator_type;
                                                   ^
/usr/include/c++/5/bits/unordered_map.h:117:45: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::pointer  pointer;
                                             ^
/usr/include/c++/5/bits/unordered_map.h:118:50: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::const_pointer const_pointer;
                                                  ^
/usr/include/c++/5/bits/unordered_map.h:119:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::reference  reference;
                                               ^
/usr/include/c++/5/bits/unordered_map.h:120:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::const_reference const_reference;
                                                    ^
/usr/include/c++/5/bits/unordered_map.h:121:46: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::iterator  iterator;
                                              ^
/usr/include/c++/5/bits/unordered_map.h:122:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::const_iterator const_iterator;
                                                   ^
/usr/include/c++/5/bits/unordered_map.h:123:51: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::local_iterator local_iterator;
                                                   ^
/usr/include/c++/5/bits/unordered_map.h:124:57: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::const_local_iterator const_local_iterator;
                                                         ^
/usr/include/c++/5/bits/unordered_map.h:125:47: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::size_type  size_type;
                                               ^
/usr/include/c++/5/bits/unordered_map.h:126:52: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       typedef typename _Hashtable::difference_type difference_type;
                                                    ^
/usr/include/c++/5/bits/unordered_map.h:280:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       operator=(initializer_list<value_type> __l)
       ^
/usr/include/c++/5/bits/unordered_map.h:379:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
  emplace(_Args&&... __args)
  ^
/usr/include/c++/5/bits/unordered_map.h:432:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       insert(const value_type& __x)
       ^
/usr/include/c++/5/bits/unordered_map.h:439:2: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
  insert(_Pair&& __x)
  ^
/usr/include/c++/5/bits/unordered_map.h:499:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       insert(initializer_list<value_type> __l)
       ^
/usr/include/c++/5/bits/unordered_map.h:645:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       equal_range(const key_type& __x)
       ^
/usr/include/c++/5/bits/unordered_map.h:649:7: error: 'value' is not a member of 'std::__not_<std::__and_<std::__is_fast_hash<PairHash<int, int> >, std::__detail::__is_noexcept_hash<std::pair<int, int>, PairHash<int, int> > > >'
       equal_range(const key_type& __x) const
       ^

What am I doing wrong?

解决方案

Apparently, libstdc++ refers to your hash object via const PairHash<int, int>*. Thus calling the operator () which is not marked const in your program is a compiler error.

You can get your code to compile with libstdc++ by making operator() const.

As of 17.6.3.4 (Hash Requirements), a Hash type must provide a size_t operator(KeyType) const;, so your code is indeed incorrect.

这篇关于使用std :: pair作为键来创建std :: unordered_map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆