C++之pair

转载自: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 进行操作。。。

  1. 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对象时,必须提供两个类型名,两个对应的类型名的类型不必相同。

声明

  1. pair<string,string>anon;  
  2. pair<string,int>word_count;  
  3. pair<string, vector<int> >line;  
pair<string,string>anon;
pair<string,int>word_count;
pair<string, vector<int> >line;

声明并初始化

  1. <pre name="code" class="plain">pair<int ,int >p (5,6);  
  2. pair<int ,int > p1= make_pair(5,6);  
  3. pair<string,double> p2 ("aa",5.0);  
  4. 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简化定义,如结构体 ) 

 
 
  1. typedef pair<string,string> Author;  
  2. Author proust("March","Proust");  
  3. Author Joy("James","Joy");  
typedef pair<string,string> Author;
Author proust("March","Proust");
Author Joy("James","Joy");

4、pair对象的操作

对于pair类,可以直接访问其数据成员:其成员都是公有的,分别命名为first和second,只需要使用普通的点操作符
 
 
  1. string firstBook;  
  2. if(author.first=="James" && author.second=="Joy")  
  3.     firstBook="Stephen Hero";  
string firstBook;
if(author.first=="James" && author.second=="Joy")
    firstBook="Stephen Hero";

5、生成新的pair对象

除了构造函数,标准库还定义了一个make_pair函数,由传递给它的两个实参生成一个新的pair对象
  
  
  1. pair<string, string> next_auth;  
  2. string first,last;  
  3. while(cin>>first>>last) {  
  4.     next_auth=make_pair(first,last);  
  5.     //...  
  6. }  
pair<string, string> next_auth;
string first,last;
while(cin>>first>>last) {
    next_auth=make_pair(first,last);
    //...
}
还可以用下列等价的更复杂的操作:
  
  
  1. next_auth=pair<string,string>(first,last);  
next_auth=pair<string,string>(first,last);
由于pair的数据成员是公有的,因而可如下直接地读取输入:
  
  
  1. pair<string, string> next_auth;  
  2. while(cin>>next_auth.first>>next_auth.last) {  
  3.     //...  
  4. }  
pair<string, string> next_auth;
while(cin>>next_auth.first>>next_auth.last) {
    //...
}
5.DEMO

  1. #include<iostream>  
  2. #include<string>  
  3. #include<vector>  
  4. #include<utility>  
  5. using namespace std;  
  6.   
  7. int main()  
  8. {  
  9.     pair<string, int>p;  
  10.     typedef vector< pair<string, int> > VP;  
  11.     VP vp;  
  12.     while(cin>>p.first>>p.second)  
  13.     {  
  14.         vp.push_back(make_pair(p.first,p.second));  
  15.           
  16.     }  
  17.     VP::iterator it;  
  18.     for(it=vp.begin(); it!=vp.end(); it++)  
  19.         cout<<it->first<<","<<it->second<<endl;  
  20.   
  21.     return 0;  
  22. }  
#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;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值