C++学习笔记之STL标准库(一)utility.h头文件即结构体模板pair

#include <utility>

pair模板类用来将两个对象表示成一个对象。

用途:1)想要函数同时返回两个参数; 2)想要用一个容器存储成对值的元素

pair模板类核心代码:

#ifndef _UTILITY_
#define _UTILITY_
#include <iosfwd>

// 结构体模板pair

template<class _Ty1,class _Ty2> struct pair

 {
 typedef _Ty1 first_type;
 typedef _Ty2 second_type;

//默认构造函数

 pair(): first(_Ty1()), second(_Ty2())
 {
 }

//以特定的值进行初始化,构造函数

 pair(const _Ty1& _Val1, const _Ty2& _Val2)
 : first(_Val1), second(_Val2)
 { 
 }

//拷贝构造函数

 template<class _Other1,class _Other2>
 pair(const pair<_Other1, _Other2>& other)
 : first(other.first), second(other.second)
 {
 }

 _Ty1 first;//成员变量,pair中的第一个值,通过成员访问运算符.来访问
 _Ty2 second; // 成员变量,pair中的第二个值
 };

// pair的模板函数和操作符重载

template<class _Ty1,class _Ty2> inline  //重载==,判断两个pair相等
 bool operator==(const pair<_Ty1, _Ty2>& x,const pair<_Ty1, _Ty2>& y)
 { 
   return (x.first == y.first && x.second == y.second);
 }

template<class _Ty1,class _Ty2> inline //重载 !=,判断两个pair不相等
 bool operator!=(const pair<_Ty1, _Ty2>& x,  const pair<_Ty1, _Ty2>& y)
 { 
   return (!(x == y));
 }

template<class _Ty1,class _Ty2> inline //重载 < ,判断两个pair大小,判断大小时,第一个元素的优先级更高
 bool operator<(const pair<_Ty1, _Ty2>& x,  const pair<_Ty1, _Ty2>& y)
 { 
   return (x.first < y.first ||  !(y.first < x.first) && x.second < y.second);
 }

template<class _Ty1,class _Ty2> inline //重载 > ,判断两个pair大小
 bool operator>(const pair<_Ty1, _Ty2>& x,  const pair<_Ty1, _Ty2>& y)
 { 
   return (y < x);
 }

template<class _Ty1,class _Ty2> inline //重载 <=
 bool operator<=(const pair<_Ty1, _Ty2>& x,  const pair<_Ty1, _Ty2>& y)
 { 
   return (!(y < x));
 }

template<class _Ty1,class _Ty2> inline //重载 >=
 bool operator>=(const pair<_Ty1, _Ty2>& x,  const pair<_Ty1, _Ty2>& y)
 { 
   return (!(x < y));
 }

template<class _Ty1,class _Ty2> inline //make_pair模板函数,常用来生成 pair对象,但注意make_pair的参数中不能有const常量,否则可能会创建失败
 pair<_Ty1, _Ty2> make_pair(_Ty1 _Val1, _Ty2 _Val2)
 { 
   return (pair<_Ty1, _Ty2>(_Val1, _Val2));
 }

#endif 

总结以上代码可发现:

1)pair支持三种构造函数进行初始化

2)pair中的一对值可以是不同的数据类型

3)pair的两个值分别通过pair.first 和 pair.second进行访问

4)常使用make_pair<class T1,class T2>(t1,t2)生成新的pair对象

5)pair支持大小比较,此时class T1与class T2两个类要相同或要支持比较大小

 

拓展:根据pair的格式写一个结构体模板trio<class _Ty1,class _Ty2,class _Ty3>,支持存储任意类型的三个对象

#ifndef _TRIO_
#define _TRIO_

#include <iosfwd>

template <class _Ty1,class _Ty2,class _Ty3> struct trio
{
   typedef _Ty1 first_type;
   typedef _Ty2 second_type;
   typedef _Ty3 third_type;

   _Ty1 first;
   _Ty2 second;
   _Ty3 third;

   //默认构造函数
   trio():first(_Ty1()),second(_Ty2()),third(_Ty3())
   {
   }

  //使用特定值进行初始化

   trio(const _Ty1& x,const _Ty2& y,const _Ty3& z):first(x),second(y),third(z)
   {
   }

  //拷贝构造函数

   template<class _Ty1,class _Ty2,class _Ty3>
   trio(const trio<_Ty1,_Ty2,_Ty3> &other):first(other.first),second(other.second),third(other.third)
   {

   }
};

//操作符==重载
template<class _Ty1,class _Ty2,class _Ty3> inline
bool operator ==(const trio<_Ty1,_Ty2,_Ty3>& x, const trio<_Ty1,_Ty2,_Ty3>& y)
{
  return ((x.first == y.first)&&(x.second == y.second)&&(x.third == y.third));
}

//模板函数,创建trio对象

template<class _Ty1,class _Ty2,class _Ty3> inline
trio<_Ty1,_Ty2,_Ty3> make_trio(const _Ty1& x,const _Ty2& y,const _Ty3& z)
{
  return trio<_Ty1,_Ty2,_Ty3>(x,y,z);
}

#endif

转载于:https://www.cnblogs.com/jason-20160301/p/8687448.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值