C++pair类型的浅析

标准库类型--pair类型定义在utility头文件中定义

 本文地址:http://www.cnblogs.com/archimedes/p/cpp-pair.html,转载请注明源地址。

1、pair的创建和初始化

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

pair<string,string>anon;
pair<string,int>word_count;
pair<string, vector<int> >line;

当然也可以在定义时为每个成员提供初始化式:

pair<string,string>author("James","Joy");

pair类型的使用相当的繁琐,如果定义多个相同的pair类型对象,可以使用typedef简化声明:

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

2、pair对象的操作

对于pair类,可以直接访问其数据成员:其成员都是公有的,分别命名为first和second,只需要使用普通的点操作符

string firstBook;
if(author.first=="James" && author.second=="Joy")
    firstBook="Stephen Hero";

3、生成新的pair对象

除了构造函数,标准库还定义了一个make_pair函数,由传递给它的两个实参生成一个新的pair对象

pair<string, string> next_auth;
string first,last;
while(cin>>first>>last) {
    next_auth=make_pair(first,last);
    //...
}

还可以用下列等价的更复杂的操作:

next_auth=pair<string,string>(first,last);

由于pair的数据成员是公有的,因而可如下直接地读取输入:

pair<string, string> next_auth;
while(cin>>next_auth.first>>next_auth.last) {
    //...
}

4、编程实践

练习:编写程序读入一系列string和int型数据,将每一组存储在一个pair对象中,然后将这些pair对象存储在vector容器

#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;
</span><span style="color:#0000ff;">for</span>(it=vp.begin(); it!=vp.end(); it++<span style="color:#000000;">)
    cout</span>&lt;&lt;it-&gt;first&lt;&lt;<span style="color:#800000;">"</span><span style="color:#800000;">,</span><span style="color:#800000;">"</span>&lt;&lt;it-&gt;second&lt;&lt;<span style="color:#000000;">endl;

</span><span style="color:#0000ff;">return</span> <span style="color:#800080;">0</span><span style="color:#000000;">;

}

        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/weixin_33717298">
                <img src="https://profile.csdnimg.cn/7/C/1/3_weixin_33717298" class="avatar_pic" username="weixin_33717298">
            </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit "><a href="https://blog.csdn.net/weixin_33717298" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}" target="_blank">weixin_33717298</a></span>
                    <!-- 等级,level -->
                                            <img class="identity-icon" src="https://csdnimg.cn/identity/blog4.png">                                            </div>
                <div class="text"><span>原创文章 146</span><span>获赞 56</span><span>访问量 24万+</span></div>
            </div>
                            <div class="right-message">
                                        <a class="btn btn-sm  bt-button personal-watch attented" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;,&quot;ab&quot;:&quot;new&quot;}">已关注</a>
                                                            <a href="https://im.csdn.net/im/main.html?userName=weixin_33717298" target="_blank" class="btn btn-sm bt-button personal-letter">私信
                    </a>
                                </div>
                        </div>
                    
    </div>

标准库类型--pair类型定义在utility头文件中定义

 本文地址:http://www.cnblogs.com/archimedes/p/cpp-pair.html,转载请注明源地址。

1、pair的创建和初始化

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

pair<string,string>anon;
pair<string,int>word_count;
pair<string, vector<int> >line;

当然也可以在定义时为每个成员提供初始化式:

pair<string,string>author("James","Joy");

pair类型的使用相当的繁琐,如果定义多个相同的pair类型对象,可以使用typedef简化声明:

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

2、pair对象的操作

对于pair类,可以直接访问其数据成员:其成员都是公有的,分别命名为first和second,只需要使用普通的点操作符

string firstBook;
if(author.first=="James" && author.second=="Joy")
    firstBook="Stephen Hero";

3、生成新的pair对象

除了构造函数,标准库还定义了一个make_pair函数,由传递给它的两个实参生成一个新的pair对象

pair<string, string> next_auth;
string first,last;
while(cin>>first>>last) {
    next_auth=make_pair(first,last);
    //...
}

还可以用下列等价的更复杂的操作:

next_auth=pair<string,string>(first,last);

由于pair的数据成员是公有的,因而可如下直接地读取输入:

pair<string, string> next_auth;
while(cin>>next_auth.first>>next_auth.last) {
    //...
}

4、编程实践

练习:编写程序读入一系列string和int型数据,将每一组存储在一个pair对象中,然后将这些pair对象存储在vector容器

#include<iostream>
#include<string>
#include<vector>
#include<utility>
using namespace std;

猜你喜欢

转载自blog.csdn.net/malloch/article/details/105887937