Pair(对组)

c++标准程序库大量运用了pair 。 例如 map 和 multimap 容器的元素型别便是pair ,也就是一组键值/实值(key/value)。

namespcae std{
template <class T1,class T2>
struct pair {
typedef T1 first_type;
typedef t2 second_type;

T1 first ;
T2 second ;
/* default constructor */
pair ()
: firts(T1()),second(T2()){
}

pair(const T1& a, const T2& b)
: first(a),second(b){
}

/* copy constructor with implicit conversions */
/* 隐式转换的拷贝构造函数 */
/* 因为pair 被定义喂struct ,而不是class 所以所有成员都是public */
template < class U, class V >
pair(const pair(U,V)& p)
: first(p.first), second(p.second){
}
};

template <class T1, class T2 >
bool operator == (const pair<T1,T2>& ,const pair<T1,T2>&);
template <class T1, class T2 >
bool operator == (const pair<T1,T2>& x,const pair<T1,T2>& y)
{
return x.first < y.first || (!(y.first<x.first) && x.second < y.second);
}

/* convenience function to create a pair */
template < class T1, class T2 >
pair <T1 ,T2> make_pair(const T1&,const T2&){
return pair<T1,T2>;
}

}



posted on 2011-11-21 13:27  xtl  阅读(240)  评论(0编辑  收藏  举报

导航