记录一下pair的用法(简单易懂)

#include<iostream>
using namespace std;

#define x first
#define y second  //宏定义,x为first,y为second
typedef int i;  //重命名,i等同于int

struct Pair //结构体
{
    int first,second;
};

struct Pair A; //变量A这个时候能够装两个int数据

pair<int,int> a; //变量a这个时候能够装两个int数据
//pair的作用是将两个数据和成一个数据,不仅限于int,可以包括很多数据类型,包括它自己,其本质也就是一个结构体。

typedef pair<int,pair<double,string>> P; //使用typedef重命名,简化代码

int main()
{
    cin>>A.first>>A.second;
    cin>>a.first>>a.second;
    //由于我们已经用x,y替换了first,second
    
    cout<<A.x<<A.y<<endl;
    cout<<a.x<<a.y<<endl;
    
    //pair也可以用初始化列表进行初始化
    pair<int,int> b{1,2};
    cout<<b.x<<b.y<<endl;
    
    //更加复杂的pair形式(套娃)
    pair< int , pair<double,string> > c { 1 , { 2.3 , "abc" } };
    cout<<c.x<<' '<<c.y.x<<' '<<c.y.y<<endl;
    
    //简化之后
    P d { 2 , { 3.2 , "def" } };
    cout<<d.x<<' '<<d.y.x<<' '<<d.y.y<<endl;
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值