#include<iostream>
#include<string>
#include<map>
using namespace std; // pair简单讲就是将两个数据整合成一个数据
// 本质上是有first, second两个成员变量的结构体
int main()
{
// pair两种构造的方法
// 方法1
pair<string, double> pA("one", 1.11);// 浮点数默认是double, float的话有会警告。 // 方法2
pair<string, int> pB;
pB = make_pair("two", 2); // pair的输出
cout << "pA : " << pA.first << " "<< pA.second << endl;
cout << "pB : " << pB.first << " "<< pB.second << endl; // 结合map的使用
map<string, double> mA;
map<string,int>mB;
mA.insert(pA);
mB.insert(pB); for (map<string, double>::iterator it = mA.begin(); it != mA.end(); ++it)
{
cout << "First Member of mA: " << it->first << endl;
cout << "Second Member of mA: " << it->second << endl;
} for (map<string, int>::iterator it = mB.begin(); it != mB.end(); ++it)
{
cout << "First Member of mB: " << it->first << endl;
cout << "Second Member of mB: " << it->second << endl;
}
return 0;
}
#include<algorithm>
#include<iostream>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<map>
#include<set>
using namespace std; map<string, int> m;
pair<string,int> p; int main() { p = make_pair("one",1);//make_pair(),返回一个pair类型 cout << p.first << endl;//输出p的key,也就是"one"; cout << p.second << endl;//输出p的value,也就是1 m.insert(make_pair("two",2)); map<string, int>::iterator mit; mit = m.begin(); cout << mit->first << endl; cout << mit->second << endl;//分别输出“two”。和2 return 0;
}

#include <iostream>
#include <string>
#include <vector>
using namespace std; int main()
{
pair<string, string> anon; // 包括两个字符串
pair<string, int> word_count; // 包括字符串和整数
pair<string, vector<int> > line; // 包括字符串和一个int容器 pair<string, string> author("James", "Joyce"); // 定义成员时初始化
cout << author.first << " - " << author.second << endl; string firstBook; // 使用 . 訪问和測试pair数据成员
if (author.first == "James" && author.second == "Joyce") {
firstBook = "Stephen Hero";
cout << firstBook << endl;
} typedef pair<string, string> Author; // 简化声明一个作者pair类型
Author proust("Marcel", "Proust");
Author Joyce("James", "Joyce"); pair<string, string> next_auth;
string first, last;
while (cin >> first >> last) {
// 使用make_pair函数生成一个新pair对象
next_auth = make_pair(first, last);
// 使用make_pair函数,等价于以下这句
next_auth = pair<string, string> (first, last); cout << next_auth.first << " - " << next_auth.second << endl;
if (next_auth.first == next_auth.second)
break; // 输入两个相等,退出循环
} cout << "由于pair的数据成员是共同拥有的。因而能够直接读取输入" << endl;
while (cin >> next_auth.first >> next_auth.second) { cout << next_auth.first << " - " << next_auth.second << endl;
if (next_auth.first == next_auth.second)
break;
} return 0;
}

临时仅仅会这么简单的使用方法。刚学到的……

C++中 pair 的使用方法的更多相关文章

  1. vector中pair的排序方法

    直接上代码: bool judge(const pair<int,char> a, const pair<int ,char> b) { return a.first<b ...

  2. c++ 中 pair 的 使用方法

    原转载地址:点击打开链接 pair的类型: pair 是 一种模版类型.每个pair 可以存储两个值.这两种值无限制.也可以将自己写的struct的对象放进去.. pair<string,int ...

  3. C++(十二)— vector中pair的排序方法

    1.利用自定义的排序函数 通过传递一个函数 cmp给sort函数 , 注意: cmp中return a<b; 决定为从小到大的排序 return a>b;  决定为从大到小的排序 #inc ...

  4. VECTOR中pair的排序

    vector中pair的排序方法 首先定义一个vector vector<pair<int,char> >vec; 调用排序函数sort sort(vec.begin(),ve ...

  5. MapReduce中一次reduce方法的调用中key的值不断变化分析及源码解析

    摘要:mapreduce中执行reduce(KEYIN key, Iterable<VALUEIN> values, Context context),调用一次reduce方法,迭代val ...

  6. JavaScript中Math对象的方法介绍

    1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...

  7. Android中锁定文件的方法

    androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...

  8. jQuery中的事件绑定方法

    在jQuery中,事件绑定方法大致有四种:bind(),live(), delegate(),和on(). 那么在工作中应该如何选择呢?首先要了解四种方法的区别和各自的特点. 在了解这些之前,首先要知 ...

  9. Eclipse中自动提示的方法参数都是arg0,arg1的解决方法

    Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...

随机推荐

  1. xss 记录cookie

    <p> <img src="http://act.ci123.com/global/ueditor_new/php/upload/98591403834900.jpg&qu ...

  2. mfs使用指引

    客户端工具集 mfsgetgoal #设定副本数 mfssetgoal #获取副本数 mfscopygoal # mfsgetsclass mfssetsclass mfscopysclass mfs ...

  3. Dvwa安装,配置(Linux)

    文章演示使用系统:CenTOS7 一:搭建LAMP环境 使用XAMPP安装部署,下载地址:https://www.apachefriends.org/download.html 1.1:赋予账号对XA ...

  4. BZOJ 4742 DP

    思路: Claris大大说了 排序以后 这个可以看成是括号序列 f[i][j][k]表示到了i j个左括号 k个右括号 (f[i][j][k]+=f[i-1][j][k])%=p; if(node[i ...

  5. B - Ultra-Fast Mathematician

    Problem description Shapur was an extremely gifted student. He was great at everything including Com ...

  6. C - Queue at the School

    Problem description During the break the schoolchildren, boys and girls, formed a queue of n people ...

  7. MapReduce架构与生命周期

    MapReduce架构与生命周期 概述:MapReduce是hadoop的核心组件之一,可以通过MapReduce很容易在hadoop平台上进行分布式的计算编程.本文组织结果如下:首先对MapRedu ...

  8. Spring DATA MongoDB @DBref查询,or和and联合查询

    @DBref文档关联,在按该类型查询的时候,在字段名后加上关联表的字段名即可,如: Criteria.where("bloggroup.$id"), $id代表关联表的oid字段. ...

  9. 图表库 - Highchart / Echart

    当前主要使用HighChart和Echart图表库,都基于Jquery,需要先引用Jquery. 实际问题:引入Jquery需在图表库前,否则报错. HighChart官网:https://www.h ...

  10. 记一次IOS对 JS的支持问题

    最终在这位博主那块找到问题https://blog.csdn.net/github_36487770/article/details/82465741 在利用Vue开发一个功能时遇到时间拼接格式化问题 ...