### 前言
? ? ?pair在關聯容器中經常被使用,它提供了兩個成員變量first和second,由于pair是一個struct,所以其成員變量的屬性是public。在pair struct中提供了構造函數和拷貝構造函數,同時提供了兩個最基本的操作operator==和operator<重載,其他的操作符重載都是基于前面兩種的變形。本文源碼來自SGI STL中的<stl_pair.h>文件。
### pair源碼剖析
~~~
#ifndef __SGI_STL_INTERNAL_PAIR_H
#define __SGI_STL_INTERNAL_PAIR_H
__STL_BEGIN_NAMESPACE
/*
pair在關聯容器中使用很廣泛,它是STL的模板類型,可以存儲兩個成員變量
pair采用的是struct結構,struct的成員默認屬性是public
*/
template <class _T1, class _T2>
struct pair {
typedef _T1 first_type;
typedef _T2 second_type;
//pair的兩個成員變量,其屬性是public
_T1 first;
_T2 second;
//以下是構造函數
pair() : first(_T1()), second(_T2()) {}
pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) {}
#ifdef __STL_MEMBER_TEMPLATES
//兼容性的拷貝構造函數
//兼容性是指兩個pair的類型可以不同,但是必須可以轉換
template <class _U1, class _U2>
pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
//注意:用pair初始化另一個pair時,只能通過拷貝構造函數進行,不能通過賦值進行
//因為這里沒有提供operator=操作符的重載
#endif
};
//operator==操作符重載
//兩個pair相等時,意味著兩個成員變量都對應相等
template <class _T1, class _T2>
inline bool operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{
return __x.first == __y.first && __x.second == __y.second;
}
//operator<操作符重載
//比較兩個pair時,以第一個成員變量first為主,若第一個成員變量first不能判斷表達式的大小
//則對其第二個成員變量second進行比較
template <class _T1, class _T2>
inline bool operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
{
return __x.first < __y.first ||
(!(__y.first < __x.first) && __x.second < __y.second);
}
#ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
//下面的操作符重載都是基于上面operator<和operator==操作符的.
//operator!=,operator>,operator<=,operator>=操作符的重載
template <class _T1, class _T2>
inline bool operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return !(__x == __y);
}
template <class _T1, class _T2>
inline bool operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return __y < __x;
}
template <class _T1, class _T2>
inline bool operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return !(__y < __x);
}
template <class _T1, class _T2>
inline bool operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return !(__x < __y);
}
#endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
//根據兩個數值,構造一個pair
template <class _T1, class _T2>
inline pair<_T1, _T2> make_pair(const _T1& __x, const _T2& __y)
{
return pair<_T1, _T2>(__x, __y);
}
__STL_END_NAMESPACE
#endif /* __SGI_STL_INTERNAL_PAIR_H */
// Local Variables:
// mode:C++
// End:
~~~
參考資料:
《STL源碼剖析》侯捷
《[STL源碼剖析-- stl_pair.h](http://blog.csdn.net/mdl13412/article/details/6643400)》
- 前言
- 空間配置器
- Traits編程技術
- STL源碼剖析——迭代器
- 全局函數construct(),destroy(),uninitialized_copy(),uninitialized_fill(),uninitialized_fill_n()
- 序列容器之vector
- list容器的排序算法sort()
- 序列容器之list
- 序列容器之deque
- 容器配接器之stack
- 容器配接器之queue
- 容器配接器之priority_queue
- 最大堆heap
- 單向鏈表slist
- RB-Tree(紅黑樹)
- 關聯容器之set
- stl_pair.h學習
- 關聯容器之map
- 關聯容器之multiset
- 關聯容器之multimap
- 散列表hashtable
- stl_hash_fun.h學習
- 關聯容器之hash_set
- 關聯容器之hash_multiset
- 關聯容器之hash_map
- 關聯容器之hash_multimap
- 數值算法stl_numeric.h
- stl_relops.h學習
- 基本算法stl_algobase.h
- STL算法之set集合算法
- STL算法stl_algo.h
- STL算法之sort排序算法
- STL算法之find查找算法
- STL算法之merge合并算法
- STL算法之remove刪除算法
- STL算法之permutation排列組合
- STL函數對象