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,发现 ...
随机推荐
- MAVEN学习笔记之私服Nexus(2)
MAVEN学习笔记之私服Nexus(2) 私有服务器搭建 Nexus www.snatype.org下载 snatype-work 是默认nexus存储nexus a:将bin添加到环境中 Admin ...
- android黑科技系列——静态分析技术来破解Apk
一.前言 从这篇文章开始我们开始我们的破解之路,之前的几篇文章中我们是如何讲解怎么加固我们的Apk,防止被别人破解,那么现在我们要开始破解我们的Apk,针对于之前的加密方式采用相对应的破解技术,And ...
- C#监测方法执行效率
System.Diagnostics.Stopwatch watch = new Stopwatch(); watch.Start(); // 开始监视代码运行时间 //需要监测的代码 dothing ...
- VHDL之Aggregate
Definition A basic operation that combines one or more values into a composite value of a record or ...
- [SOA]REST与SOA两种架构的异同比较
REST的特性 它基于HTTP协议,是一种明确构建在客户端/服务端体系结构上的一种风格.特征如下: 1.网络上的资源都被抽象为资源,这些资源都具有唯一的统一资源标识符(URI:Uniform Reso ...
- Python 3 print 函数用法总结
Python 3 print 函数用法总结 1. 输出字符串和数字 print("runoob") # 输出字符串 runoob print(100) ...
- python tips:生成器的小问题
在Python中,生成器和函数很像,都是在运行的过程中才会去确定各种变量的值,所以在很多情况下,会导致各种各样的问题. def generator_test1(): # 0...9 generator ...
- VMware虚拟机共享文件夹问题: /mnt下没有hgfs文件夹
在使用vmware虚拟机共享文件夹功能的时候,发现在/mnt目录下面没有hgfs文件夹,但是vmware-tool的命令vmhgfs-fuse确实存在于系统中.在使用vmhgfs-fuse建立宿主机到 ...
- mac安装win10后触摸板没有右键功能键的添加技巧
一些mac用户也会在自己的笔记本电脑上安装windows10系统. 但最近有部分用户发现,安装上win10正式版后,发现无论点击触摸板哪个位置,都只有左键,根本无法右键的问题, 针对此问题,现笔者分享 ...
- 洛谷 P1540 乌龟棋
第一感觉是定义状态f[n][i][j][k][kk],但这样空间和时间都承受不下.我们可以设状态为f[i][j][k][kk],这样可以省掉一个n,因为我们依据行走步数可以直接算出行走距离. Code ...