转载自:http://www.cnblogs.com/archimedes/p/cpp-pair.html
http://www.cnblogs.com/Nimeux/archive/2010/10/05/1844191.html
std::pair主要的作用是将两个数据组合成一个数据,两个数据可以是同一类型或者不同类型。例如std::pair<int,float> 或者 std::pair<double,double>等。pair实质上是一个结构体,其主要的两个成员变量是first和second,这两个变量可以直接使用。初始化一个pair可以使用构造函数,也可以使用std::make_pair函数,make_pair函数的定义如下:
1.应用
如果一个函数有两个返回值 的话,如果是相同类型,就可以用数组返回,如果是不同类型,就可以自己写个struct ,但为了方便就可以使用 c++ 自带的pair ,返回一个pair,其中带有两个值。除了返回值的应用,在一个对象有多个属性的时候 ,一般自己写一个struct ,如果就是两个属性的话,就可以用pair 进行操作。。。
- template pair make_pair(T1 a, T2 b) { return pair(a, b); }
template pair make_pair(T1 a, T2 b) { return pair(a, b); }
2.标准库类型--pair类型定义在utility头文件中定义
3.声明和初始化
pair包含两个数值,与容器一样,pair也是一种模板类型。但是又与之前介绍的容器不同,在创建pair对象时,必须提供两个类型名,两个对应的类型名的类型不必相同。
声明
- pair<string,string>anon;
- pair<string,int>word_count;
- pair<string, vector<int> >line;
pair<string,string>anon;
pair<string,int>word_count;
pair<string, vector<int> >line;
声明并初始化
- <pre name="code" class="plain">pair<int ,int >p (5,6);
- pair<int ,int > p1= make_pair(5,6);
- pair<string,double> p2 ("aa",5.0);
- pair <string ,double> p3 = make_pair("aa",5.0);
<pre name="code" class="plain">pair<int ,int >p (5,6);
pair<int ,int > p1= make_pair(5,6);
pair<string,double> p2 ("aa",5.0);
pair <string ,double> p3 = make_pair("aa",5.0);
pair类型的使用相当的繁琐,如果定义多个相同的pair类型对象,可以
使用typedef简化声明:(其他的自定义类型也可以使用typedef简化定义,如结构体 )
- typedef pair<string,string> Author;
- Author proust("March","Proust");
- Author Joy("James","Joy");
typedef pair<string,string> Author; Author proust("March","Proust"); Author Joy("James","Joy");
4、pair对象的操作
对于pair类,可以直接访问其数据成员:其成员都是公有的,分别命名为first和second,只需要使用普通的点操作符
- string firstBook;
- if(author.first=="James" && author.second=="Joy")
- firstBook="Stephen Hero";
string firstBook; if(author.first=="James" && author.second=="Joy") firstBook="Stephen Hero";
5、生成新的pair对象
除了构造函数,标准库还定义了一个make_pair函数,由传递给它的两个实参生成一个新的pair对象
还可以用下列等价的更复杂的操作:
- pair<string, string> next_auth;
- string first,last;
- while(cin>>first>>last) {
- next_auth=make_pair(first,last);
- //...
- }
pair<string, string> next_auth; string first,last; while(cin>>first>>last) { next_auth=make_pair(first,last); //... }
由于pair的数据成员是公有的,因而可如下直接地读取输入:
- next_auth=pair<string,string>(first,last);
next_auth=pair<string,string>(first,last);
- pair<string, string> next_auth;
- while(cin>>next_auth.first>>next_auth.last) {
- //...
- }
pair<string, string> next_auth; while(cin>>next_auth.first>>next_auth.last) { //... }
- #include<iostream>
- #include<string>
- #include<vector>
- #include<utility>
- using namespace std;
- int main()
- {
- pair<string, int>p;
- typedef vector< pair<string, int> > VP;
- VP vp;
- while(cin>>p.first>>p.second)
- {
- vp.push_back(make_pair(p.first,p.second));
- }
- VP::iterator it;
- for(it=vp.begin(); it!=vp.end(); it++)
- cout<<it->first<<","<<it->second<<endl;
- return 0;
- }
#include<iostream>
#include<string>
#include<vector>
#include<utility>
using namespace std;
int main()
{
pair<string, int>p;
typedef vector< pair<string, int> > VP;
VP vp;
while(cin>>p.first>>p.second)
{
vp.push_back(make_pair(p.first,p.second));
}
VP::iterator it;
for(it=vp.begin(); it!=vp.end(); it++)
cout<<it->first<<","<<it->second<<endl;
return 0;
}