C++编程->pair(对组)
pair 是 一种模版类型。每一个pair 能够存储两个值。这两种值无限制,能够是tuple。vector ,string,struct等等。
首先来看一下pair的函数
初始化。复制等相关操作例如以下:
default (1) |
constexpr pair(); |
---|---|
copy / move (2) |
template<class U, class V> pair (const pair<U,V>& pr); |
initialization (3) |
pair (const first_type& a, const second_type& b); |
piecewise (4) |
template <class... Args1, class... Args2> |
// pair TEMPLATE FUNCTIONS
//交换函数
template<class _Ty1,
class _Ty2> inline
void swap(pair<_Ty1, _Ty2>& _Left, pair<_Ty1, _Ty2>& _Right)
{ // swap _Left and _Right pairs
_Left.swap(_Right);
}
//推断是否相等函数
template<class _Ty1,
class _Ty2> inline
bool operator==(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test for pair equality
return (_Left.first == _Right.first && _Left.second == _Right.second);//两个元素都比較
}
//推断是否不等函数
template<class _Ty1,
class _Ty2> inline
bool operator!=(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test for pair inequality
return (!(_Left == _Right));
}
//推断是否小于函数
template<class _Ty1,
class _Ty2> inline
bool operator<(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left < _Right for pairs
return (_Left.first < _Right.first ||
!(_Right.first < _Left.first) && _Left.second < _Right.second);
}
//推断是否大于函数
template<class _Ty1,
class _Ty2> inline
bool operator>(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left > _Right for pairs
return (_Right < _Left);
}
//推断是否小于等于函数
template<class _Ty1,
class _Ty2> inline
bool operator<=(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left <= _Right for pairs
return (!(_Right < _Left));
}
//推断是否大于等于函数
template<class _Ty1,
class _Ty2> inline
bool operator>=(const pair<_Ty1, _Ty2>& _Left,
const pair<_Ty1, _Ty2>& _Right)
{ // test if _Left >= _Right for pairs
return (!(_Left < _Right));
}
贴一段代码:
//pair 定义
pair<string,int>pair1; //pair 定义以及赋值一
pair<string,int>pair2("lily",4);
pair<string,int>pair3(pair2); //pair 赋值方式二
pair1=make_pair(string("tom"),3); //pair 赋值方式三
pair1.first="jim";
pair1.second=2; //pair 赋值方式四
get<0>(pair1)=string("jim");
get<1>(pair1)=6; //pair 赋值方式五
swap(pair1,pair3); //pair 输出方式一
cout<<pair2.first<<endl;
cout<<pair2.second<<endl; //pair 输出方式二
cout<<get<0>(pair1)<<endl;
cout<<get<1>(pair1)<<endl;
C++编程->pair(对组)的更多相关文章
- pair 对组
pair 对组 c++ 基础 2016-05-10 19:42 154人阅读 评论(0) 收藏 举报 分类: 头文件的函数精粹(12) 版权声明:本文为博主原创文章,未经博主允许不得转载. 与关联 ...
- Unix/Linux环境C编程入门教程(35) 编程管理系统中的组
组管理相关函数介绍 相关函数 getgid,setgid,setregid 表头文件 #include<unistd.h> #include<sys/types.h> 定 ...
- linux网络编程之一-----多播(组播)编程
什么是多播 组播(Multicast)是网络一种点对多(one to many)的通信方式,通过报文复制完成网络中一台server对应多台接收者的高效数据传 送.对其形象的比喻就是类似于广播电台和电视 ...
- STL之pair对组
#include<iostream> #include<algorithm> #include<cstring> #include<cstdlib> u ...
- pair对组
一.pair基本概念 对组(pair)将一对值组合成一个值,这一对值可以具有不同的数据类型,两个值可以分别用pair的两个公有函数first和second访问. 类模板:template <cl ...
- 结对编程-Core 第12组 [pb15061359+pb15061351]
一.项目要求 1.输入题目数量,生成操作数为3~5个的四则运算题目 2.输入上限值控制生成的操作数的最大值以及结果的最大值 3.输入支持的操作符类型:加.减.乘.除.乘方.括号 4.输入支持的操作数类 ...
- 编程算法 - 连续子数组的最大和 代码(C)
连续子数组的最大和 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 题目: 输入一个整型数组, 数组里有正数也有负数. 数组中一个或连续的多个整数组成一 ...
- Linux编程 16 文件权限(组管理 groupadd, groupmod,文件权限介绍)
一.用户组 前面章节知道用户账户在控制单个用户安全性方面很好,但涉及到共享资源或把用户类型分组时,组概念就出来了. 组权限允许多个用户对系统中的对象(比如文件,目录,设备等)共享一组共用的权限. 在c ...
- 屏幕编程 F4的帮组用法
PROCESS ON VALUE-REQUEST. * 设置帮助(工作中心) FIELD wa_zppt026-arbpl MODULE mdl_arbpl_f4. *&---------- ...
随机推荐
- Gym - 101981J The 2018 ICPC Asia Nanjing Regional Contest J.Prime Game 计数
题面 题意:1e6的数组(1<a[i]<1e6), mul (l,r) =l × (l+1) ×...× r, fac(l,r) 代表 mul(l,r) 中不同素因子的个数,求s ...
- Aspnet_Session
cmd: aspnet_regsql.exe -ssadd -sstype c -d ZZCasSession -S 192.168.0.3 -U sa -P szhweb2010 <!--会话 ...
- Ubuntu下搭建repo服务器(一): 配置gitosis
1. 说明 服务器端IP: 192.168.1.126,下文简称:A端: 客户端IP: 192.168.130.19,下文简称:B端: Android工程代号:17435. 2. 安装必要软件(A端) ...
- [转载]Android平台第三方应用分享到微信开发
一.申请APPID 微信公共平台和微博分享一样,也需要申请一个ID,来作为调起微信.分享到微信的唯一标识. 申请微信APPID可以到微信平台http://open.weixin.qq.com/app/ ...
- SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs
ORACLE-BASE - ALTER TABLE ... SHRINK SPACE Command : Online Segment Shrink for Tables, LOBs and IOTs ...
- 【java基础】(6)内部类
内部类不是很好理解,但说白了其实也就是一个类中还包含着另外一个类 如同一个人是由大脑.肢体.器官等身体结果组成,而内部类相当于其中的某个器官之一,例如心脏:它也有自己的属性和行为(血液.跳动) 显然, ...
- 偶尔遇到的“The request was aborted:Could not create SSL/TLS secure channel.”怎么解决?
项目中涉及到调用第三方的Https的WebService,我使用的是原始的HttpWebRequest. 代码中已经考虑到是Https,加上了SSL3协议,加上了委托调用.但偶尔还是会碰到 The r ...
- Python之global
1 Global The global statement and its nonlocal cousin are the only things that are remotely like dec ...
- Git及Github环境搭建(Windows系统)
一.github账号注册 1.打开网址https://github.com 注册账号: 二.本地安装Git 1.安装包下载地址:链接:https://pan.baidu.com/s/1smpnJL7 ...
- android全屏下的输入框未跟随软键盘弹起问题
最近开发中遇到,全屏模式下输入框在底部不会跟随软键盘弹起.于是网上搜索了解决的方案.大致找到了两种方案. 第一种 定义好此类 public class SoftKeyBoardListener { p ...