pair 类型

一、pair的理解

可以把pair理解成C中的sprintf,存放链接两种类型数据,访问第一种使用first,访问第二种数据使用second。


二、pair的使用


基本使用:

	#include <iostream>
	#include <utility>
	
	using namespace  std;
	
	int main()
	{
	    pair <string,string>  student(小明,三班);
	
	    cout<<student.first<<student.second<<endl;
	
	    return 0;
	}

运行结果:

小明三班

pair与类结合使用:以下实例用test类对象作为pair的第二个存储数据,同时使用到了构造函数二号this指针。

	#include <iostream>
	#include <utility>
	
	using namespace  std;
	class test
	{
	public:
	    string str_data;
	    test(string str_data)
	    {
	        this->str_data = str_data;//初始化成员变量str_data;
	        cout<<str_data<<endl;
	    }
	};
	
	int main()
	{
	    string str_data = "hello";
	    test test_one("world");
	    pair <string,test> pair_test(str_data,test_one);
	
	    cout<<pair_test.first<<pair_test.second.str_data<<endl;//输出pair变量中的数据
	    
	    return 0;
	}

输出结果:

world
helloworld
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值