C++ 使用std::pair作为键创建std::无序_映射
我正在尝试创建一个std::unordered_映射,并将std::pair作为键。可以想象,这需要我显式地提供一个类来为给定的键生成哈希,并为键提供一个相等比较器。以下是我目前的代码:C++ 使用std::pair作为键创建std::无序_映射,c++,c++11,C++,C++11,我正在尝试创建一个std::unordered_映射,并将std::pair作为键。可以想象,这需要我显式地提供一个类来为给定的键生成哈希,并为键提供一个相等比较器。以下是我目前的代码: #include <unordered_map> #include <memory> #include <utility> template <class T, typename U> struct PairHash{ size_t operator
#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;
}
#包括
#包括
#包括
模板
结构PairHash{
size_t运算符()(const std::pair&key){
返回std::hash()(key.first)^std::hash()(key.second);
}
};
模板
结构PairEqual{
布尔运算符()(常数std::pair&lhs,常数std::pair&rhs)常数{
返回lhs.first==rhs.first&&lhs.second==rhs.second;
}
};
结构图形边缘{
};
int main(){
std::无序的_映射边;
}
然而,这给了我一个相当(至少在我看来)难以理解的编译器错误:
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
^
在/usr/include/c++/5/bits/hashtable.h:35:0中包含的文件中,
从/usr/include/c++/5/无序映射:47,
来自项目cpp:1:
/usr/include/c++/5/bits/hashtable_policy.h:在“struct std::_detail::_是_noexcept_hash”的实例化中:
/usr/include/c++/5/type_traits:137:12:必须来自“struct std::u和u”
/usr/include/c++/5/type_traits:148:38:从“struct std::_not_”中需要
/usr/include/c++/5/bits/unordered_-map.h:100:66:class std::unordered_-map中的必填项
程序cpp:29:43:从这里开始需要
/usr/include/c++/5/bits/hashtable_policy.h:85:34:错误:调用“(const PairHash)(const std::pair&)”不匹配
无例外(declval()(declval())>
^
prog.cpp:7:12:注:候选:size\t PairHash::operator()(const std::pair&)[带t=int;U=int;size\U t=unsigned int]
size_t运算符()(const std::pair&key){
^
prog.cpp:7:12:注意:将“const PairHash*”传递为“this”参数将丢弃限定符
在/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,
来自项目cpp:1:
/usr/include/c++/5/type_traits:在“struct std::__not_”的实例化中:
/usr/include/c++/5/bits/unordered_-map.h:100:66:class std::unordered_-map中的必填项
程序cpp:29:43:从这里开始需要
/usr/include/c++/5/type_traits:148:38:错误:“value”不是“std::u和_u”的成员
:公共积分常数
^
在/usr/include/c++/5/unordered_map:48:0中包含的文件中,
来自项目cpp:1:
/usr/include/c++/5/bits/unordered_-map.h:在“class std::unordered_-map”的实例化中:
程序cpp:29:43:从这里开始需要
/usr/include/c++/5/bits/unordered\u map.h:100:66:错误:“value”不是“std:”的成员
typedef_uuumap_hashtable_hashtable;
^
/usr/include/c++/5/bits/unordered\u map.h:107:45:错误:“value”不是“std:”的成员
typedef typename\u哈希表::key\u type key\u type;
^
/usr/include/c++/5/bits/unordered\u map.h:108:47:错误:“value”不是“std:”的成员
typedef typename_Hashtable::value_type value_type;
^
/usr/include/c++/5/bits/unordered\u map.h:109:48:错误:“value”不是“std:”的成员
typedef typename_Hashtable::mapped_type mapped_type;
^
/usr/include/c++/5/bits/unordered\u map.h:110:43:错误:“value”不是“std:”的成员
typedef typename_Hashtable::hasher-hasher;
^
/usr/include/c++/5/bits/unordered\u map.h:111:46:错误:“value”不是“std:”的成员
typedef typename_Hashtable::key_equal key_equal;
^
/usr/include/c++/5/bits/unordered\u map.h:112:51:错误:“value”不是“std:”的成员
typedef typename\u哈希表::分配器\u类型分配器\u类型;
^
/usr/include/c++/5/bits/unordered\u map.h:117:45:错误:“value”不是“std:”的成员
typedef typename_哈希表::指针;
^
/usr/include/c++/5/bits/unordered\u map.h:118:50:错误:“value”不是“std:”的成员
typedef typename_哈希表::常量指针常量指针;
^
/usr/include/c++/5/bits/unordered\u map.h:119:47:错误:“value”不是“std:”的成员
typedef typename_Hashtable::reference;
^
/usr/include/c++/5/bits/unordered\u map.h:120:52:错误:“value”不是“std:”的成员
typedef typename_哈希表::const_reference const_reference;
^
/usr/include/c++/5/bits/unordered\u map.h:121:46:错误:“value”不是“std:”的成员
typedef typename_Hashtable::迭代器迭代器;
^
/usr/include/c++/5/bits/unordered\u map.h:122:51:错误:“value”不是“std:”的成员
typedef typename_哈希表::常量迭代器常量迭代器;
^
/usr/include/c++/5/bits/unordered\u map.h:123:51:错误:“value”不是“std:”的成员
typedef typename_Hashtable::local_iterator local_iterator;
^
/usr/include/c++/5/bits/unordered\u map.h:124:57:错误:“value”不是“std:”的成员
typedef typename_Hashtable::const_local_迭代器const_local_迭代器;
^
/usr/include/c++/5/bits/unordered\u map.h:125:47:错误:“value”不是“std:”的成员
typedef typename\u哈希表::size\u type size\u type;