STL Source Code Analysis # Stl_pair #

Source: Internet
Author: User

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&LT;_T1, _t2>& __x, const PAIR&LT;_T1, _t2&gt ;& __y) {return __x.first = = __y.first && __x.second = __y.second;} Template <class _t1, class _t2>inline bool operator< (const PAIR&LT;_T1, _t2>& __x, const PAIR&LT;_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&LT;_T1 , _t2>& __x, const PAIR&LT;_T1, _t2>& __y) {return! __x = = __y);} Template <class _t1, class _t2>inline bool Operator> (const PAIR&LT;_T1, _t2>& __x, const PAIR&LT;_T1, _t2& gt;& __y) {return __y < __x;} Template <class _t1, class _t2>inline bool operator<= (const PAIR&LT;_T1, _t2>& __x, const PAIR&LT;_T1, _T2 >& __y) {return! ( __y < __x);} Template <class_T1, class _t2>inline bool operator>= (const PAIR&LT;_T1, _t2>& __x, const PAIR&LT;_T1, _t2>& __y) {R Eturn! (__x < __y);}


The Make_pair function, in essence, calls the constructor ....













STL Source Code Analysis # Stl_pair #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.