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:
| Default (1) |
CONSTEXPR pair (); |
copy/move (2)
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<_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<_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;