Pair类型概述

pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下:

pair<int, string> a;

表示a中有两个类型,第一个元素是int型的,第二个元素是string类型的,如果创建pair的时候没有对其进行初始化,则调用默认构造函数对其初始化。

pair<string, string> a("James", "Joy");

也可以像上面一样在定义的时候直接对其初始化。

由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:

typedef pair<string, string> author;

author pro("May", "Lily");

author joye("James", "Joyce");

Pair对象的操作

  • 对于pair类,由于它只有两个元素,分别名为first和second,因此直接使用普通的点操作符即可访问其成员

pair<string, string> a("Lily", "Poly");

string name;

name = pair.second;

  • 生成新的pair对象

可以使用make_pair对已存在的两个数据构造一个新的pair类型:

int a = 8;

string m = "James";

pair<int, string> newone;

newone = make_pair(a, m);

1#include <iostream>
2#include <utility>
3#include <string>
4usingnamespace std;
5
6int main () {
7pair <string,double> product1 ("tomatoes",3.25);
8pair <string,double> product2;
9pair <string,double> product3;
10
11product2.first ="lightbulbs"; // type of first is string
12product2.second =0.99; // type of second is double
13
14product3 = make_pair ("shoes",20.0);
15
16cout <<"The price of "<< product1.first <<" is $"<< product1.second <<"\n";
17cout <<"The price of "<< product2.first <<" is $"<< product2.second <<"\n";
18cout <<"The price of "<< product3.first <<" is $"<< product3.second <<"\n";
19return0;
20}
其运行结果如下:
1The price of tomatoes is $3.25
2The price of lightbulbs is $0.99
3The price of shoes is $20
 
 
 
 
 
01 pair  vs  make_pair
02 make_pair constructs a pair object.
03 template
04 pair make_pair(T1 x, T2 y)
05 {
06     return pair(x, y);
07 }
08
09 eg:  std::pair("sn001", 12.5);
10        std::make_pair("sn001", 12.5);
11        两者效果一样。
12 倘若:std::pair("sn002", 12.6);   // 12.6's datatype is float
13         std::make_pair("sn002",12.6);  // 12.6's datatype is double
14 使用:
15         std::pair m_pairA;
16         m_pairA = std::make_pair("sn001", 12.5);
17         std::cout<<m_pairA.first<<"  "<<m_pairA.second<<std::endl;
18 结合map的简单使用:
19         std::pair m_pairA;
20         m_pairA = std::make_pair("sn001", 12.5);
21         //std::cout<<m_pairA.first<<"  "<<m_pairA.second<<std::endl;
22         std::map m_mapA;
23         m_mapA.insert(m_pairA);
24         std::map::iterator iter = m_mapA.begin();
25         std::cout<<iter->first<<"  "<<iter->second<<std::endl;
小结:
  make_pair创建的是一个pair对象。使用都很方便,针对成对出现的数据,如书的ISBN对应一个书名。
  pair是单个数据对的操作,pair是一struct类型,有两个成员变量,通过first,second来访问,用的是“.”访问。
  map是一个关联容器,里面存放的是键值对,容器中每一元素都是pair类型,通过map的insert()方法来插入元素(pair类型)。

[ZZ] C++ pair的更多相关文章

  1. pat1091-1100

    1091bfs傻逼题,dfs会爆栈 #include<iostream> #include<cstdio> #include<cstring> #include&l ...

  2. The Balance(poj2142)

    The Balance Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 5452   Accepted: 2380 Descr ...

  3. Queue Pair in RDMA (zz)

    Queue Pair in RDMA 首页分类标签留言关于订阅2018-03-21 | 分类 Network  | 标签 RDMA 一个CA(Channel Adapter)可以包含多个QP,QP相当 ...

  4. c++ pair 使用

    1. 包含头文件: #include <utility> 2. pair 的操作: pair<T1,T2> p; pair<T1,T2> p(v1,v2); pai ...

  5. 论Pair的重要性

    这些天我在用React和D3做图表,从已经实现的图表里复制了一些坐标轴的代码,发现坐标轴上的n个点里,只有第一个点下面能渲染出文字提示,其余点下面都无法渲染出文字. 和组里的FL一起百思不得其解好几天 ...

  6. 2016 ACM/ICPC Asia Regional Dalian Online 1010 Weak Pair dfs序+分块

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submissio ...

  7. pair的使用

    #include<iostream> #include<cmath> #include<cstdio> #include<algorithm> #inc ...

  8. 【C++】pair

    STL的pair,有两个值,可以是不同的类型. template <class T1, class T2> struct pair; 注意,pair在头文件utility中,不要inclu ...

  9. hackerrank Similar Pair

    传送门 Problem Statement You are given a tree where each node is labeled from 1 to n. How many similar ...

随机推荐

  1. lightOJ 1366 Pair of Touching Circles(统计矩形内相切圆对)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1366 题意:给出一个矩形,在内部画两个圆A和B使得AB都完全在矩形内且AB相切且AB的 ...

  2. SGU 385 Highlander(期望)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=385 题意: 思路: double f[N][N][N],g[N][N],A[N][ ...

  3. 【笨嘴拙舌WINDOWS】窗体样式

    "眼睛是人类心灵的窗口,打开窗口,你就能看到整个世界" 在PC时代,计算机的显示屏就是真个世界,WINDOWS将真个世界分解为一个个的窗口,每个窗口有自己的容貌,下面我们将一一揭开 ...

  4. Qt之HTTPS登录

    简述 HTTPS(全称:Hyper Text Transfer Protocol over Secure Socket Layer),是以安全为目标的HTTP通道,简单讲是HTTP的安全版.即HTTP ...

  5. iPad中控制器view初始的width和height

    1> 规律 * width 是宽高中最小的那个值 * height 是宽高中最大的那个值 2> 举例(比如窗口根控制器的view,有状态栏的情况下) * 横屏  width = 748,h ...

  6. [Mac][MySQL]如何启动MySQL Server

    方法来自 MySQL 5.7官方手册 http://dev.mysql.com/doc/refman/5.7/en/osx-installation-launchd.html 有两种方法,另一种是命令 ...

  7. C的结构体使用

    C的结构体演示 #include <stdio.h> struct A //建立结构体A { char *name; int s1; struct A *next; }; void mai ...

  8. USACO 2014 Open Silver Fairphoto

    这道题只是银牌组的第一题而我就写了 3K 的代码.唉. Description - 问题描述 FJ's N cows (2 <= N <= 100,000) are standing at ...

  9. 关于UNION ALL与 UNION 用法和区别

    (转自:http://www.cnblogs.com/EricaMIN1987_IT/archive/2011/01/20/1940188.html) UNION指令的目的是将两个SQL语句的结果合并 ...

  10. (转)每天一个Linux命令(8): tar

    通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar命令可以为linux ...