pair< int,int>对作为unordered_map问题的关键 [英] pair<int,int> pair as key of unordered_map issue

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

问题描述

我的代码:

  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'

我需要修复?



感谢

解决方案

是没有专门为 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;
}
};


my code:

 typedef pair<int,int> Pair
  tr1::unordered_map<Pair,bool> h;
  h.insert(make_pair(Pair(0,0),true));

erorr

 undefined reference to `std::tr1::hash<std::pair<int, int> >::operator()(std::pair<int, int>) const'

something i need to fix?

thanks

解决方案

This happens because there is no specialization for 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>.

Following there is a example of how to specialize 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;
        }
};

这篇关于pair&lt; int,int&gt;对作为unordered_map问题的关键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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