C++中 pair 的使用方法
#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 的使用方法的更多相关文章
- vector中pair的排序方法
直接上代码: bool judge(const pair<int,char> a, const pair<int ,char> b) { return a.first<b ...
- c++ 中 pair 的 使用方法
原转载地址:点击打开链接 pair的类型: pair 是 一种模版类型.每个pair 可以存储两个值.这两种值无限制.也可以将自己写的struct的对象放进去.. pair<string,int ...
- C++(十二)— vector中pair的排序方法
1.利用自定义的排序函数 通过传递一个函数 cmp给sort函数 , 注意: cmp中return a<b; 决定为从小到大的排序 return a>b; 决定为从大到小的排序 #inc ...
- VECTOR中pair的排序
vector中pair的排序方法 首先定义一个vector vector<pair<int,char> >vec; 调用排序函数sort sort(vec.begin(),ve ...
- MapReduce中一次reduce方法的调用中key的值不断变化分析及源码解析
摘要:mapreduce中执行reduce(KEYIN key, Iterable<VALUEIN> values, Context context),调用一次reduce方法,迭代val ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- Android中锁定文件的方法
androidSDK中并没有锁定文件相关的api. 但是android是基于linux操作系统的,linux比较底层,灵活性也更大,为了实现锁定文件的效果,大概有以下几种办法: 用chmod命令修改文 ...
- jQuery中的事件绑定方法
在jQuery中,事件绑定方法大致有四种:bind(),live(), delegate(),和on(). 那么在工作中应该如何选择呢?首先要了解四种方法的区别和各自的特点. 在了解这些之前,首先要知 ...
- Eclipse中自动提示的方法参数都是arg0,arg1的解决方法
Eclipse中自动提示的方法参数都是arg0,arg1,就不能根据参数名来推断参数的含义,非常不方便. 解决方法:Preferences->Java->Installed JREs,发现 ...
随机推荐
- 搭建自己的websocket server_1
用Node.js实现一个WebSocket的Server. https://github.com/sitegui/nodejs-websocket#event-errorerr nodejs-we ...
- 备份SQL SERVER 2005数据库
- [CF1139 E] Maximize Mex 解题报告 (二分图匹配)
interlinkage: https://codeforces.com/contest/1139/problem/E description: 有$n$个学生,$m$个社团,每个学生有一个能力值,属 ...
- nginx配置访问密码,让用户输入用户名密码才能访问
如果我们在 nginx 下搭建了一些站点,但是由于站点内容或者流量的关系,我们并不想让所有人都能正常访问,那么我们可以设置访问认证.只有让用户输入正确的用户名和密码才能正常访问.效果如下: 在 ngi ...
- Java Break和continue实现goto功能
continue实验 1 public class test { static int i =0; public static void main(String[] args) { lable1: w ...
- Hibernate中实体对象的状态
实体对象的状态 这里的实体对象是指Hibernate的O/R映射关系中的域对象(即O/R中的O).实体对象的生命周期是指实体对象由产生到被GC回收的一段过程,实体对象的生命周期包括3种状态:自由状态( ...
- [ Linux ] [ OS ] [ memory ] Linux 如何查看系統硬體的記憶體(RAM)資訊
cat /proc/meminfo https://blog.longwin.com.tw/2013/05/linux-ram-memory-info-2013/
- Find Bugs
为什么没有早点知道有这么好用的插件呢?
- 使用序列号激活优动漫PAINT(附激活码)
优动漫PAINT是一款功能强大的动漫绘图软件,简单的中文界面和丰富的笔刷操纵,再次为设计工作者带来非一般的感受!最近,有不少小伙伴提出这样的疑问:购买安装优动漫PAINT之后,不知道如何激活,在哪里输 ...
- 从无到有创建一个grunt项目
在安装好grunt的前提下创建一个grunt的项目: 1.首先创建一个项目文件 就叫grunt-project 2.进入这个文件 创建一个index.html 在创建一个js文件,进去创建一个inde ...