C++标准库---pair用法及实现

pair的类型:

   pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下:

    pair<string,int> p;

    pair<int ,int > p;

    pair<double,int> p;

也可以将自己写的struct的对象放进去。

应用:如果一个函数有两个返回值的话,如果是相同类型,就可以用数组返回,如果是不同类型,就可以自己写个struct,但为了方便就可以使用c++自带的pair,返回一个pair,其中带有两个值.除了返回值的应用,在一个对象有多个属性的时候 ,一般自己写一个struct ,如果就是两个属性的话,就可以用pair进行操作。尤其像容器类别map和mulitmap,就是使用pairs来管理其键值/实值(key/value)的成对元素。任何函数需要返回两个值,也需要pair。


代码示例:

#include<iostream>
#include<string>
#include<utility>

using namespace std;

void f(pair<int,const char*>a)
{
	cout<<a.first<<" "<<a.second<<endl;
}
void g(pair<int,string>a)
{
	a.first+=1;
	cout<<a.first<<" "<<a.second<<endl;
}

void foo()
{
	pair<int,const char*> p(42,"hello");
	f(p);
	g(p);//template 构造函数隐式转换
	cout<<p.first<<" "<<p.second<<endl;
}

int main()
{
	//定义
	pair<int,string> s(100,"xiaoming"),str;

	cout<<s.first<<" "<<s.second<<endl;

	str=s;

	cout<<str.first<<" "<<str.second<<endl;


	//多个形式相同的pair
	typedef pair<int,string> author;

    author a(101,"lanzhihui");
	author b(102,"wangdan");
	author c;

	c=make_pair(103,"wangqian");

	cout<<a.first<<" "<<a.second<<endl;
	cout<<b.first<<" "<<b.second<<endl;
	cout<<c.first<<" "<<c.second<<endl;

	//template形式的构造函数
	foo();

	system("pause");
	return 0;
}
运行结果:




实现pair内部如何实现?

#include<string>
#include<iostream>

using namespace std;

template <class T1,class T2>
struct my_pair
{
	typedef T1 first_type;
	typedef T2 second_type;

	T1 first;
	T2 second;

	//默认构造函数
	my_pair()
		:first(T1()),second(T2()) {}  //T1()用0初始化 T2()用0初始化

	//构造函数
	my_pair(const T1&a,const T2&b)
		:first(a),second(b) {}

	//拷贝构造函数
	template<class U,class V>
	my_pair(const my_pair<U,V>&p)
		:first(p.first),second(p.second) {cout<<"a"<<endl;}//{用来测试那些对象创建调用了copy构造函数}
};

//比较
template<class T1,class T2>
bool operator==(const my_pair<T1,T2>&s1,const my_pair<T1,T2>&s2)
{
	return s1.first==s2.first&&s1.second==s2.second;
}

template<class T1,class T2>
bool operator>(const my_pair<T1,T2>&s1,const my_pair<T1,T2>&s2)
{
	return (s1.first>s2.first)||(!(s1.first<s2.first)&&s1.second>s2.second);
}

template<class T1,class T2>
bool operator<(const my_pair<T1,T2>&s1,const my_pair<T1,T2>&s2)
{
	return (s1.first<s2.first)||(!(s1.first>s2.first)&&s1.second<s2.second);
}

int main()
{
	//默认构造函数
	my_pair<int,string> a2;
	
	cout<<"默认构造函数:"<<endl;
	cout<<a2.first<<" "<<a2.second<<endl;

	//构造函数
	my_pair<int,char*> a1(101,"lanzhihui");//参数类型必须与定义类型一样

	cout<<"构造函数:"<<endl;
	cout<<a1.first<<" "<<a1.second<<endl;

	my_pair<int,char*> a3(101,"laznzhihui");
	cout<<a3.first<<" "<<a3.second<<endl;

	//拷贝构造函数
	my_pair<int,string> s1(a1),s2(a1);//使用Template形式的copy构造函数,构造过程隐式类型转换

	cout<<"拷贝构造函数:"<<endl;
	cout<<s1.first<<" "<<s1.second<<endl;
	cout<<s2.first<<" "<<s2.second<<endl;
    
	my_pair<int,string> s3;
	
	cout<<"sss"<<endl;
	s3=a3;//也是调用拷贝构造函数
	cout<<s3.first<<" "<<s3.second<<endl;
	cout<<"sss"<<endl;

	cout<<"测试:"<<endl;
	//测试'==' '>' '<'
	if(s1==s2)
	{
		cout<<"operator==   OK!"<<endl;
	}

	if(s1<s3)
	{
		cout<<"operator>   OK!"<<endl;
	}

	if(s3>s1)
	{
		cout<<"operator<   OK!"<<endl;
	}

	system("pause");
	return 0;
}

运行结果:



  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值