zoukankan      html  css  js  c++  java
  • 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); }  
    2.标准库类型--pair类型定义在utility头文件中定义

    3.声明和初始化

    pair包含两个数值,与容器一样,pair也是一种模板类型。但是又与之前介绍的容器不同,在创建pair对象时,必须提供两个类型名,两个对应的类型名的类型不必相同。

    声明

    1. pair<string,string>anon;  
    2. pair<string,int>word_count;  
    3. 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);  
    pair类型的使用相当的繁琐,如果定义多个相同的pair类型对象,可以使用typedef简化声明:(其他的自定义类型也可以使用typedef简化定义,如结构体 )

    [plain] view plain copy 在CODE上查看代码片派生到我的代码片
    1. typedef pair<string,string> Author;  
    2. Author proust("March","Proust");  
    3. Author Joy("James","Joy");  

    4、pair对象的操作

    对于pair类,可以直接访问其数据成员:其成员都是公有的,分别命名为first和second,只需要使用普通的点操作符
    [plain] view plain copy 在CODE上查看代码片派生到我的代码片
    1. string firstBook;  
    2. if(author.first=="James" && author.second=="Joy")  
    3.     firstBook="Stephen Hero";  

    5、生成新的pair对象

    除了构造函数,标准库还定义了一个make_pair函数,由传递给它的两个实参生成一个新的pair对象
    [plain] view plain copy 在CODE上查看代码片派生到我的代码片
    1. pair<string, string> next_auth;  
    2. string first,last;  
    3. while(cin>>first>>last) {  
    4.     next_auth=make_pair(first,last);  
    5.     //...  
    6. }  
    还可以用下列等价的更复杂的操作:
    [plain] view plain copy 在CODE上查看代码片派生到我的代码片
    1. next_auth=pair<string,string>(first,last);  
    由于pair的数据成员是公有的,因而可如下直接地读取输入:
    [plain] view plain copy 在CODE上查看代码片派生到我的代码片
    1. pair<string, string> next_auth;  
    2. while(cin>>next_auth.first>>next_auth.last) {  
    3.     //...  
    4. }  
    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;  

  • 相关阅读:
    How to build Linux system from kernel to UI layer
    Writing USB driver for Android
    Xposed Framework for Android 8.x Oreo is released (in beta)
    Linux Smartphone Operating Systems You Can Install Today
    Librem 5 Leads New Wave of Open Source Mobile Linux Contenders
    GUADEC: porting GNOME to Android
    Librem 5 – A Security and Privacy Focused Phone
    GNOME and KDE Join Librem 5 Linux Smartphone Party
    Purism计划推出安全开源的Linux Librem 5智能手机
    国产系统之殇:你知道的这些系统都是国外的
  • 原文地址:https://www.cnblogs.com/zhangmingzhao/p/7256681.html
Copyright © 2011-2022 走看看