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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM