pair的定义C++

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

  T1 first;
  T2 second;

  pair() :first(T1()), second(T2()) {}
  pair(const T1& x, const T2& y) :first(x), second(y) {}
  template<class U, class V>
  pair(const pair<U, V>& p) :first(p.first), second(p.second) {}
};

对任何pair,我们总用firstsecond索引其第一个和第二个元素,无论其类型是什么。

方便地创建pair的函数:

template <class T1, class T2> pair<T1, T2> std::make_pair(T1 &t1, T2 &t2) {

  return pair<T1, T2>(t1, t2);

}

在默认情况下,pair将被其元素类型的默认值进行初始化。特别的,这也就意味着将内部类型

的元素初始化为0,将string初始化为空串。如果一个类型没有默认构造函数,要将它作为pair

的元素时,就必须显式提供创建pair的参数。

原文地址:https://www.cnblogs.com/donggongdechen/p/9607351.html