C++ pair的用法

本文介绍了C++中的pair类模板,详细讲解了pair的特点,包括默认的排序方式,并阐述了pair作为容器用于存储不同数据类型的两个值的能力。通过first和second属性,可以方便地访问这两个值。
摘要由CSDN通过智能技术生成

头文件:
    #include <vector>
特点:
    pair默认对first升序,当first相同时对second升序。
类模板:
    template<class T1,class T2>struct pair
参数:
    T1:第一个参数的数据类型;
    T2:第二个参数的数据类型。
功能:
    pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1,T2);
    两个值可以分别用共用函数first和second访问。
具体用法:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
 
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
//根据first的值进行升序排序 
bool cmp_first_up(pair<int,double> a, pair<int,double> b)
{
	return a.first < b.first;
}
//根据second的值进行升序排序
bool cmp_second_up(pair<int,double> a,pair<int,double> b)
{
	return a.second < b.second;
} 
int main(int argc, char** argv) {
	//1.定义 
	pair <int,double> p1;//使用默认
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值