pair:

头文件:#include<utility>

类模板:template <class T1, class T2> struct pair

参数:T1是第一个值的数据类型,T2是第二个值的数据类型。

功能:pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1和T2),两个值可以分别用pair的两个公有函数first和second访问。

具体用法:

1.实例化:

  

[cpp] view plaincopy print?
  1. pair<string,string> p1("hello","word"); //调用default constructor
  2. pair<double,int>    p2(1.0,1);//调用constructor
  3. pair<double,int>    p3(p2); //调用 copy
   pair<string,string> p1("hello","word"); //调用default constructorpair<double,int>    p2(1.0,1);//调用constructorpair<double,int>    p3(p2); //调用 copy

2.对象的赋值以及make_pair()的应用:

[cpp] view plaincopy print?
  1. pair<string,string> p1;
  2. pair<string,string> p2("good","good");
  3. p1= p2;
  4. p1= make_pair("hello","word");
  5. p1 = pair<string,string>("nice","nice");
     pair<string,string> p1;pair<string,string> p2("good","good");p1= p2;p1= make_pair("hello","word");p1 = pair<string,string>("nice","nice");

3.pair中元素的访问(first & second):

[cpp] view plaincopy print?
  1. pair<double,int>    p1(1.0,2);
  2. pair<string,string> p2("hello","word");
  3. int i = p1.second;  // i = 2
  4. double d = p1.first; // d = 1.0
  5. string s1 = p2.first; // s1 = hello
  6. string s2 = p2.second; // word
     pair<double,int>    p1(1.0,2);pair<string,string> p2("hello","word");int i = p1.second;  // i = 2double d = p1.first; // d = 1.0string s1 = p2.first; // s1 = hellostring s2 = p2.second; // word

4.pair数组与元素排序:

[cpp] view plaincopy print?
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<algorithm>
  4. using namespace std;
  5. pair<int,int>pa[100];
  6. int cmp(pair<int,int>a,pair<int,int>b){
  7. if(a.first!=b.first)return a.first>b.first;
  8. else return a.second<b.second;
  9. }
  10. int main(){
  11. int a,b;
  12. for(int i=0;i<5;i++)scanf("%d%d",&a,&b),pa[i]=make_pair(a,b);
  13. sort(pa,pa+5,cmp);
  14. for(int i=0;i<5;i++)printf("%d %d\n",pa[i].first,pa[i].second);
  15. return 0;
  16. }
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
pair<int,int>pa[100];
int cmp(pair<int,int>a,pair<int,int>b){if(a.first!=b.first)return a.first>b.first;else return a.second<b.second;
}
int main(){int a,b;for(int i=0;i<5;i++)scanf("%d%d",&a,&b),pa[i]=make_pair(a,b);sort(pa,pa+5,cmp);for(int i=0;i<5;i++)printf("%d %d\n",pa[i].first,pa[i].second);return 0;
}

c++ 中pair类模板的用法详解相关推荐

  1. C++ - 类模板(class template) 详解 及 代码

    类模板(class template) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/16906827 类模板(c ...

  2. python3 isinstance用法_对python中assert、isinstance的用法详解

    1. assert 函数说明: Assert statements are a convenient way to insert debugging assertions into a program ...

  3. python中tile的用法_python3中numpy函数tile的用法详解

    tile函数位于python模块 numpy.lib.shape_base中,他的功能是重复某个数组.比如tile(A,n),功能是将数组A重复n次,构成一个新的数组,我们还是使用具体的例子来说明问题 ...

  4. Python 3.7.1 中 namedtuple 具名元组基本用法详解

    Python 3.7.1 中 namedtuple 具名元组基本用法详解 转载请注明出处:https://blog.csdn.net/jpch89/article/details/84645251 文 ...

  5. C语言getline函数CSDN,c++中的两种getline用法详解

    getline是C++标准库函数:但不是C标准库函数,而是POSIX(IEEE Std 1003.1-2008版本及以上)所定义的标准库函数(在POSIX IEEE Std 1003.1-2008标准 ...

  6. python的raw_ input是什么意思-对python中raw_input()和input()的用法详解

    最近用到raw_input()和input()来实现即时输入,就顺便找了些资料来看,加上自己所用到的一些内容,整理如下: 1.raw_input() raw_input([prompt]) -> ...

  7. python中str和input_对python中raw_input()和input()的用法详解

    最近用到raw_input()和input()来实现即时输入,就顺便找了些资料来看,加上自己所用到的一些内容,整理如下: 1.raw_input() raw_input([prompt]) -> ...

  8. js数组中foEach和map的用法详解 jq中的$.each和$.map

    数组中foEach和map的用法详解 相同点: 1.都是循环遍历数组(仅仅是数组)中的每一项. 2.forEach() 和 map() 里面每一次执行匿名函数都支持3个参数:数组中的当前项value, ...

  9. 教程-Delphi中Spcomm使用属性及用法详解

    Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...

最新文章

  1. runtime 日志权限不够_MySQL权限处理的一个小bug
  2. STM32很难?这些工具可以帮大忙!
  3. c++ linux 获取毫秒_Linux c++获取本地毫秒级精确时间
  4. HDFS +zookeeper实现高可用
  5. python smtplib发送邮件可直接运行代码
  6. 即时通讯领域必将最终由XMPP协议一统天下
  7. 搭建基于XMPP的即时通讯系统
  8. EDEM颗粒堆积fluent meshing网格生成
  9. 50个最常用的Unix/Linux命令
  10. 计算机内存与速度,内存容量与读写速度
  11. 龙芯源码编译mysql_龙芯服务器安装总结
  12. 【知识兔】六个Excel生成序号技巧,超级大技巧
  13. 【STM32CubeMX安装】
  14. eclipse网络代理设置
  15. Android Audio(七)—— AudioFocus(duck)
  16. UTC秒数转换成时间
  17. 2553: 谁是赢家
  18. 常用的几种GPRS模块介绍 GPRS模块选哪个好
  19. android判断银行卡号格式不正确,android银行卡号验证算法 android银行卡号验证算法详解...
  20. mac mini安装ubuntu系统

热门文章

  1. android 活动说明,Android – 如何发送GCM推送通知以及要加载哪些活动的说明?
  2. 2021-01-16 英文写作中“因为”
  3. JDK1.8中的Stream详解
  4. linux在执行时提示无此文件的一个处理办法
  5. 最短路问题之Bellman-ford算法
  6. 通过 Java 线程堆栈进行性能瓶颈分析
  7. Vue.js组件学习
  8. 发布在IIS的网站,可以用本机IP登录访问,用localhost不可登录访问
  9. C# winform DataGridView 属性说明
  10. 不信任的 .exe 怎么办,用 Windows 沙盒啊!