C + + Programming->pair (for groups)

Source: Internet
Author: User

pair is a template type. Each pair can store two values. These two values are unlimited, which can be tuple,vector, string,struct, and so on.

First look at the function of pair

initialization, replication, and other related operations are as follows:

copy/move (2)
Default (1)
CONSTEXPR pair ();
template<class U, class v> pair (const pair<u,v>& PR); template<class U, Class V > Pair (pair<u,v>&& PR);p Air (const pair& PR) = Default;pair (pair&& pr) = default; 
initialization (3)
pair (const first_type& A, const second_type& b); Template<class U, Class v> pair (U&
& A, v&& b); 
Piecewise (4)
Template <class ... ARGS1, class ... args2>  pair (piecewise_construct_t PwC, tuple<args1...> First_args,                                   tuple<args2...> second_ args);

Pair TEMPLATE functions//interchange function Template<class _ty1,class _ty2> inlinevoid swap (pair<_ty1, _ty2>& _Left , Pair<_ty1, _ty2>& _right) {//swap _left and _right pairs_left.swap (_right);} Determine if the equality function Template<class _ty1,class _ty2> inlinebool operator== (const pair<_ty1, _ty2>& _Left,const Pair<_ty1, _ty2>& _right) {//test for pair equalityreturn (_left.first = = _right.first && _left.second = = _right.second);//Two elements are compared}//to determine whether unequal functions Template<class _ty1,class _ty2> inlinebool operator!= (const pair<_ty1, _ty2>& _left,const pair<_ty1, _ty2>& _right) {//test for pair Inequalityreturn (! ( _left = = _right));} Determine if the function is less than template<class _ty1,class _ty2> inlinebool operator< (const pair<_ty1, _ty2>& _Left,const Pair<_ty1, _ty2>& _right) {//test if _left < _right for Pairsreturn (_left.first < _right.first | |! (_right.first < _left.first) && _left.second < _right.second);} Determine if the function is greater thanTemplate<class _ty1,class _ty2> inlinebool operator> (const pair<_ty1, _ty2>& _Left,const pair<_ Ty1, _ty2>& _right) {//test if _left > _right for Pairsreturn (_right < _left);} Determines whether the function is less than or equal to Template<class _ty1,class _ty2> inlinebool operator<= (const pair<_ty1, _ty2>& _Left, Const PAIR&LT;_TY1, _ty2>& _right) {//test if _left <= _right for Pairsreturn (! ( _right < _left));} Determine if the function is greater than or equal to Template<class _ty1,class _ty2> inlinebool operator>= (const pair<_ty1, _ty2>& _Left, Const PAIR&LT;_TY1, _ty2>& _right) {//test if _left >= _right for Pairsreturn (! ( _left < _right));}

Post a piece of code:

Pair defines the Pair<string,int>pair1;//pair definition and assigns a pair<string,int>pair2 ("Lily", 4);p air<string,int> PAIR3 (PAIR2);//pair Assignment mode two Pair1=make_pair (String ("Tom"), 3),//pair assignment method three pair1.first= "Jim";p Air1.second=2;//pair Assignment Mode four get<0> (PAIR1) =string ("Jim");get<1> (PAIR1) =6;//pair assignment mode five swap (PAIR1,PAIR3);//pair output mode one cout< <pair2.first<<endl;cout<<pair2.second<<endl;//pair Output Mode two cout<<get<0> (PAIR1) <<endl;cout<<get<1> (PAIR1) <<endl;


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.