C++ pair 用法

来源:互联网 发布:软件著作权zhongzhiip 编辑:程序博客网 时间:2024/06/17 23:38

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

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

1.定义:

#include<iostream>using namespace std;pair<int,int> sta1;


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


pair<int,int> q1;pair<int ,string> q2;pair<double,string> q3;


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


q.push(pa(x,y));q.front().first;q.front().second;


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


pair<int,int> sta[1001];pair<int,int> tmp;sta[i]=tmp;


5.相关简化声明。


typedef pair<string, string> author;author pro("May", "Lily");author joye("James", "Joyce");


6.极端用法?


pair<int,pair<int,int> >  //两个>之间要有空格

1 0