c++ pair 用法

来源:互联网 发布:淘客查询软件 编辑:程序博客网 时间:2024/06/08 07:17

来源:点击打开链接

pair相当于是包含有两个变量的struct,同样类型的pair变量可以直接赋值,这里比struct要方便,写的时候也很简单,确实很好用,而且first和second可以用代码补全,不用每次都自己打~

pair使用头文件iostream,记得要声明using namespace std;

1.定义:

[cpp] view plain copy
  1. #include<iostream>  
  2. using namespace std;  
  3.   
  4. pair<int,int> sta1;  


2.pair<>中的变量类型可以是不同的,double,string等等都可以放进去。


[cpp] view plain copy
  1. pair<int,int> q1;  
  2. pair<int ,string> q2;  
  3. pair<double,string> q3;  


3.pair可以使变量强制转化。


[cpp] view plain copy
  1. q.push(pa(x,y));  
  2. q.front().first;q.front().second;  


4.pair可以同类型之间互相赋值,不用声明first之类。


[cpp] view plain copy
  1. pair<int,int> sta[1001];  
  2. pair<int,int> tmp;  
  3. sta[i]=tmp;  


5.相关简化声明。


[cpp] view plain copy
  1. typedef pair<string, string> author;  
  2. author pro("May""Lily");  
  3. author joye("James""Joyce");  


6.极端用法?


[cpp] view plain copy
  1. pair<int,pair<int,int> >  //两个>之间要有空格