pair(c++)

头文件

#include<utility>

 模板

template<class T1,class T2>struct pair;

成员类型 定义 笔记
first_type 第一个模板参数 (T1) 成员类型 .first
second_type 第二个模板参数 (T2) 成员类型 .second

构造函数(c++11)

默认 (1)
constexpr pair();
复制/移动 (2)
template<class U, class V> pair (const pair<U,V>& pr);
template<class U, class V> pair (pair<U,V>&& pr);
pair (const pair& pr) = default;
pair (pair&& pr) = default;
初始化 (3)
pair (const first_type& a, const second_type& b);
template<class U, class V> pair (U&& a, V&& b);
分段 (4)
template <class... Args1, class... Args2>
  pair (piecewise_construct_t pwc, tuple<Args1...> first_args,
                                   tuple<Args2...> second_args);

示例

pair <int, int> pair1;   //调用默认构造函数
pair <int, int> pair2(2,5)    //用两个值来初始化
pair <int, int> pair3(pair2)   //调用复制构造函数
pair1.first=3;  //手动赋值
pair1.second=6  //手动赋值
*********************************************************
输入:
cout<<pair2.first<<endl;
cout<<pair2.second;

输出:
2
5

猜你喜欢

转载自blog.csdn.net/m0_63024355/article/details/129783123
今日推荐