C++ pair笔记

c++中的结构模板,定义在头文件中,提供一个包含2个数据成员的结构体模板。继承与_Pair_base结构体模板。通过first,second访问2个成员,有 operator= 和 swap 方法。

1.模板

template <class T1,class T2> struct pair;

2.构造函数

2.1 默认构造函数

pair();
构造一个 对对象及其元素值初始化默认值。

2.2 复制构造函数

template<class U, class V> pair (const pair<U,V>& pr);
该对象使用pr的内容进行初始化 对pair。pr
的相应成员被传递给每个成员的构造函数。

2.3 初始化构造函数

pair (const first_type& a, const second_type& b)
成员变量first为a,成员变量second为b。

2.4例子
// pair::pair example
#include <utility>      // std::pair, std::make_pair
#include <string>       // std::string
#include <iostream>     // std::cout

int main () {
  std::pair <std::string,double> product1;                     // default constructor
  std::pair <std::string,double> product2 ("tomatoes",2.30);   // value init
  std::pair <std::string,double> product3 (product2);          // copy constructor

  product1 = std::make_pair(std::string("lightbulbs"),0.99);   // using make_pair (move)

  product2.first = "shoes";                  // the type of first is string
  product2.second = 39.90;                   // the type of second is double

  std::cout << "The price of " << product1.first << " is $" << product1.second << '\n';
  std::cout << "The price of " << product2.first << " is $" << product2.second << '\n';
  std::cout << "The price of " << product3.first << " is $" << product3.second << '\n';
  return 0;
}

3.成员函数(不包含构造函数)

3.1 swap(pair)

交换两pair内容

#include <utility>      // std::pair
#include <iostream>     // std::cout

int main () {
  std::pair<int,char> foo (10,'a');
  std::pair<int,char> bar (90,'z');

  swap (foo,bar);

  std::cout << "foo contains: " << foo.first;
  std::cout << " and " << foo.second << '\n';
//foo包含:90和z
  return 0;
}
3.2 pair& operator= (const pair& pr)

将pr 深复制

#include <utility>      // std::pair, std::make_pair
#include <string>       // std::string
#include <iostream>     // std::cout

int main () {
  std::pair <std::string,int> planet, homeplanet;

  planet = std::make_pair("Earth",6371);

  homeplanet = planet;

  std::cout << "Home planet: " << homeplanet.first << '\n';
  std::cout << "Planet size: " << homeplanet.second << '\n';
  return 0;
}

4.非成员函数

4.1 void swap (pair<T1,T2>& x, pair<T1,T2>& y)

交换 x 和 y


#include <utility>      // std::pair
#include <iostream>     // std::cout

int main () {
 std::pair<int,char> foo (10,'a');
 std::pair<int,char> bar (90,'z');

 swap (foo,bar);

 std::cout << "foo contains: " << foo.first;
 std::cout << " and " << foo.second << '\n';

 return 0;
}


4.2 get(pair) (tuple接口)

根据pair中的位置获取元素,0返回first,1返回second。

#include <utility>      // std::pair, std::get
#include <iostream>     // std::cout

int main () {
  std::pair <int,char> foo (10,'x');

  std::get<0>(foo) = 50;

  std::cout << "foo contains: ";
  std::cout << std::get<0>(foo) << " and " << std::get<1>(foo) << '\n';
//foo contains: 50 and x

  return 0;
}

4.3 make_pair

构造pair
源码:

template <class T1,class T2>
  pair<T1,T2> make_pair (T1 x, T2 y)
  {
    return ( pair<T1,T2>(x,y) );
  }

例子

#include <utility>      // std::pair
#include <iostream>     // std::cout

int main () {
  std::pair <int,int> foo;
  std::pair <int,int> bar;

  foo = std::make_pair (10,20);
  bar = std::make_pair (10.5,'A'); // ok: implicit conversion from pair<double,char>

  std::cout << "foo: " << foo.first << ", " << foo.second << '\n';
  std::cout << "bar: " << bar.first << ", " << bar.second << '\n';

  return 0;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值