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,发现 ...
随机推荐
- angular2 使用swiper
欢迎加入前端交流群交流知识&&获取视频资料:749539640 第一步: npm install swiper --save 第二步:下载swiper ts支持(http://micr ...
- Maven与IDEA结合
转自:https://blog.csdn.net/zzpzheng/article/details/49474671 1. 什么是 Maven,为什么要使用 Maven 而不是 Ant Maven简单 ...
- Serializable-源码分析
package java.io; public interface Serializable { } 代码很简单,功能也很简单,对象通过这个接口来实现序列化和反序列的.下面来看看小例子. import ...
- 【BZOJ1010】【HNOI2008】玩具装箱toy (斜率优化DP) 解题报告
题目: 题目在这里 思路与做法: 这题不难想. 首先我们先推出一个普通的dp方程: \(f_i = min \{ f_j+(i-j-1+sum_i-sum_j-L)^2\}\) 然后就推一推式子了: ...
- POJ 2406 KMP next数组的应用
题意:让你找最小重复串的个数 加深KMP中对next数组的理解 #include <cstdio> #include <cstring> using namespace std ...
- 如何在在页面中清除一个已知的cookie?
前些天在写一个项目的时候,使用cookie来存储一些用户数据,在用户登出时需要清理以往的数据,对于一个初学者来说,我需要学习如何清除一个已知的cookie. 首先,引入两个js文件: 1.jquery ...
- javascript中的构造函数和原型及原型链
纯属个人理解,有错误的地方希望大牛指出,以免误人子弟 1.构造函数: 构造函数的作用 : 初始化由new创建出来的对象 new 的作用: 创建对象(空对象) new 后面跟的是函数调用,使用ne ...
- ComboBoxEdit 添加键值
ComboBoxEdit combo = new ComboBoxEdit(); var coll = combo.Properties.Items; coll.BeginUpdate(); try ...
- Django[pronounced dʒ] installation on windows
1.Install python, download python windows installer from http://www.python.org/download/ and do inst ...
- nyoj___大数阶乘
http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数阶乘 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 我们都知 ...