pair< int,int>对作为unordered_map问题的关键 [英] pair<int,int> pair as key of unordered_map issue
问题描述
我的代码:
typedef pair< int,int& Pair
tr1 :: unordered_map< Pair,bool> H;
h.insert(make_pair(Pair(0,0),true));
erorr
未定义引用`std :: tr1 :: hash< std :: pair< int,int> > :: operator()(std :: pair< int,int>)const'
我需要修复?
感谢
是没有专门为 下面是一个如何使 my code: erorr something i need to fix? thanks This happens because there is no specialization for Following there is a example of how to specialize
这篇关于pair< int,int>对作为unordered_map问题的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! std :: tr1 :: hash< Key>
与 Key = std :: pair< int,int>
。
您必须专门化 std :: tr1 :: hash< Key>
和 Key = std :: pair< int,int& / code>之前声明
tr1 :: unordered_map< Pair,bool> h;
。
这是因为 std
不知道如何散列对< int,int>
。 / p>
std :: tr1 :: hash<>
模板<>
struct std :: tr1 :: hash< std :: pair< int,int> > {
public:
size_t operator()(std :: pair< int,int> x)const throw(){
size_t h = SOMETHING; // something with x
return h;
}
};
typedef pair<int,int> Pair
tr1::unordered_map<Pair,bool> h;
h.insert(make_pair(Pair(0,0),true));
undefined reference to `std::tr1::hash<std::pair<int, int> >::operator()(std::pair<int, int>) const'
std::tr1::hash<Key>
with Key = std::pair<int, int>
.
You must to specialize std::tr1::hash<Key>
with Key = std::pair<int, int>
before declaring tr1::unordered_map<Pair,bool> h;
.
This happens because std
don't know how to hash a pair<int, int>
.std::tr1::hash<>
template <>
struct std::tr1::hash<std::pair<int, int> > {
public:
size_t operator()(std::pair<int, int> x) const throw() {
size_t h = SOMETHING;//something with x
return h;
}
};