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,发现 ...
随机推荐
- mvc:view-controller直接转发页面
在springMVC中,通过@RequestMapping发送请求地址,转发到目标页面,但是,有时候想直接访问页面, 不想通过xxx.jsp直接访问页面,可以通过springmvc.xml配置文件中的 ...
- 什么是Ajax和JSON,他们的优缺点?
ajax的概念:ajax是一种通过后台与服务器进行少量的数据交换,实现页面异步更新 是一种创建交互式网页应用的网页开发技术. json的概念:json是一种轻量级的数据交换格式,具有良好的可读和便于快 ...
- Codeforces Round #198 (Div. 2)C,D题解
接着是C,D的题解 C. Tourist Problem Iahub is a big fan of tourists. He wants to become a tourist himself, s ...
- resgen.exe 已退出 代码为 1073741701的错误的解决办法
以管理员的身份打开命令提示窗口:(开始-运行-cmd),更改目录至"CD C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\bin\”下 ...
- js编写时间选择框
效果图: 代码: 新建js:WdatePicker.js /* * My97 DatePicker 4.72 Release * License: http://www.my97.net/dp/lic ...
- 将mysql默认编码改为UTF8
windows: a. WIN+R net stop mysql 关闭mysql服务 b. 复制my-dafault.ini,重命名为my.ini,进入里面 UBUNTU: ** sudo vim ...
- PowerDesigner 逆向工程 Could not Initialize JavaVM!
原项目的大量的表,使用PowerDesigner 进行逆向工程.提示Could not Initialize JavaVM! 网上找到原因,PowerDesigner 不可以使用64位JDK环境! 有 ...
- 位姿检索PoseRecognition:LSH算法.p稳定哈希
位姿检索使用了LSH方法,而不使用PNP方法,是有一定的来由的.主要的工作会转移到特征提取和检索的算法上面来,有得必有失.因此,放弃了解析的方法之后,又放弃了优化的方法,最后陷入了检索的汪洋大海. 0 ...
- <td colspan="6"></td>代表这个td占6个td的位置
<td colspan="6"><span class="order-time">2017-10-11 14:55:23</spa ...
- 树(6)-----DFS
1.二叉树的反向层次遍历 def levelOrderBottom1(self, root): res = [] self.dfs(root, 0, res) return res def dfs(s ...