STL Source Code Analysis # Stl_pair #
Pair is a very simple and commonly used associative container (associative container).
/***********************************************programmer:eofdate:2015.04.10file:pair.cppe-mail:[email protected]***********************************************/#include <iostream> #include <string>using namespace Std;int Main (int argc, char const *argv[]) {pair<int, int> Int_pair (+);p air<string, int> si_pair ("Hello World");p air<string, string> ss_pair ("Jason", "Leaster"); cout << Int_pair.first << "<< int_pair.second << endl;cout << si_pair.first <<" "<< si_pair.second
<< endl;cout << ss_pair.first << "" << ss_pair.second << endl;return 0;}
It's hard. The data member of the pair data type is not actually a private member ~
Let's look at the pair implementation:
This is the simplest data abstraction I have seen in STL so far ...
The various comparison operators supported:
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;} Template <class _t1, class _t2>inline bool operator< (const PAIR<_T1, _t2>& __x, const PAIR<_T1, _t2& gt;& __y) {return __x.first < __y.first | | (! (__y.first < __x.first) && __x.second < __y.second); } #ifdef __stl_function_tmpl_partial_ordertemplate <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& gt;& __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) {R Eturn! (__x < __y);}
The Make_pair function, in essence, calls the constructor ....
STL Source Code Analysis # Stl_pair #