现在的位置: 首页 > 综合 > 正文

STL赏析——pair

2017年11月05日 ⁄ 综合 ⁄ 共 2638字 ⁄ 字号 评论关闭

STL赏析——pair

复制代码
 1 #ifndef __SGI_STL_INTERNAL_PAIR_H
2 #define __SGI_STL_INTERNAL_PAIR_H
3
4 __STL_BEGIN_NAMESPACE
5
6 template <class _T1, class _T2>
7 struct pair {
8 typedef _T1 first_type;
9 typedef _T2 second_type;
10
11 _T1 first;
12 _T2 second;
13 pair() : first(_T1()), second(_T2()) {}
14 pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) {}
15
16 #ifdef __STL_MEMBER_TEMPLATES
17 template <class _U1, class _U2>
18 pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
19 #endif
20 };
21
22 template <class _T1, class _T2>
23 inline bool operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
24 {
25 return __x.first == __y.first && __x.second == __y.second;
26 }
27
28 template <class _T1, class _T2>
29 inline bool operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
30 {
31 return __x.first < __y.first ||
32 (!(__y.first < __x.first) && __x.second < __y.second);
33 }
34
35 #ifdef __STL_FUNCTION_TMPL_PARTIAL_ORDER
36
37 template <class _T1, class _T2>
38 inline bool operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
39 return !(__x == __y);
40 }
41
42 template <class _T1, class _T2>
43 inline bool operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
44 return __y < __x;
45 }
46
47 template <class _T1, class _T2>
48 inline bool operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
49 return !(__y < __x);
50 }
51
52 template <class _T1, class _T2>
53 inline bool operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
54 return !(__x < __y);
55 }
56
57 #endif /* __STL_FUNCTION_TMPL_PARTIAL_ORDER */
58
59 template <class _T1, class _T2>
60 inline pair<_T1, _T2> make_pair(const _T1& __x, const _T2& __y)
61 {
62 return pair<_T1, _T2>(__x, __y);
63 }
64
65 __STL_END_NAMESPACE
66
67 #endif /* __SGI_STL_INTERNAL_PAIR_H */
复制代码

决定写几篇STL小内容的赏析。首先从pair下手,pair比较短小,相对好写一些。

1. pair是一个模板类。在pair的开始首先对类型进行了typedef。

这也是STL内部经常使用到的手法,我们可以通过一些技术来直接获得pair的内部类型。

typedef _T1 first_type;
typedef _T2 second_type;

然后是pair的3个构造函数。

复制代码
pair() : first(_T1()), second(_T2()) {}
pair(const _T1& __a, const _T2& __b) : first(__a), second(__b) {}

#ifdef __STL_MEMBER_TEMPLATES
template <class _U1, class _U2>
pair(const pair<_U1, _U2>& __p) : first(__p.first), second(__p.second) {}
#endif
复制代码

第一个构造函数,使用了初始化列表,非常方便的使用了_T1(),_T2()这样的构造函数,特别说明的是,他对基础类型也是有效的。

然后是一个2参数的构造函数。特别注意的是他要求参数是相同的。

然后是一个模板成员构造函数,该参数有一个特别的地方,就是能进行隐式类型转换。

例如

pair<int, char* > p1;
pair<int, string> p2 = p1;

这样的代码就是使用了第三个构造函数来完成的。

在我们看一个类的构造函数是还特别需要注意的就是系统为每个类加了一个默认拷贝构造函数,而且其不被成员模板构造函数覆盖!!。例如说在参数调用且类型一致时时使用的就是默认的拷贝构造函数。C++的这个性能,方便了用户,减少了coder的负担,但是同样增大的风险。类似的性能以及更复杂的性能也是C++一直遭受吐槽的原因之一……

接下来是各种比较操作符的重载(符号重载也是C++的吐槽点)。

我们需要特别注意的是下面的所有过程完全是使用 == 和<来完成的。

复制代码
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);
}
复制代码

在 __y.first < __x.first 这里也是使用小于号。

实现了 == 和 <后其他的都可以通过 这2者实现了。当然我们要保证类型_T1, _T2的满足必要的特性。

然后就是make_pair,方便使用,比较简单。

抱歉!评论已关闭.