Utilities之pairs

来源:互联网 发布:大华ipc onvif协议端口 编辑:程序博客网 时间:2024/06/03 18:04
类Pair是一个结构体,它是把两个元素作为一个单元。在STL中在很多地方会用到,特别是容器类:map,multimap会用Pair来管理数据,pair中的两个元素分别是key/value。它被定义在<utilities>中。
  namespace std {
template <class T1, class T2>
struct pair {
   typedef T1 first_type;
   typedef T2 second_type;
   T1 first;
   T2 second;
   pair(): first(T1()), second(T2()) {}//默认构造函数,看起来很奇怪,其实是正确的。
   pair(const T1& a, const T2& b): first(a), second(b) {}
   template<class U, class V>
   pair(const pair<U,V>& p): first(p.first), second(p.second) {}
};
//comparisons
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>&, const pair<T1,T2>&);
... //similar: !=, <=, >, >=
template <class T1, class T2>
pair<T1,T2> make_pair (const T1&, const T2&);
}
这些函数都是被定义在std中,我们可以直接用的,例如:
我们可以用std::make_pair(42, ‘0’)来代替 std::pair<int,char>(42,'0')等等。
总之这个类很简单。
0 0
原创粉丝点击