C ++ programming-) pair (peer group)
Pair is a template type. Each pair can store two values. These two values are not limited. They can be tuple, vector, string, struct, and so on.
First, let's look at the pair function.
Initialization, replication, and other related operations are as follows:
| Default (1) |
constexpr pair(); |
| Copy/move (2) |
template
pair (const pair
& pr);template
pair (pair
&& pr);pair (const pair& pr) = default;pair (pair&& pr) = default;
|
| Initialization (3) |
pair (const first_type& a, const second_type& b);template
pair (U&& a, V&& b);
|
| Piecewise (4) |
template
pair (piecewise_construct_t pwc, tuple first_args, tuple second_args);
|
// Pair template functions // exchange function template
Inlinevoid swap (pair <_ Ty1, _ Ty2> & _ Left, pair <_ Ty1, _ Ty2> & _ Right) {// swap _ Left and _ Right pairs_Left.swap (_ Right);} // judge whether the function template is equal
Inlinebool operator = (const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test for pair equalityreturn (_ Left. first = _ Right. first & _ Left. second = _ Right. second); // both elements are compared} // judge whether the function template is not equal
Inlinebool operator! = (Const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test for pair inequalityreturn (! (_ Left = _ Right);} // determines whether the value is smaller than the function template.
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);} // checks whether the value is greater than the function template.
Inlinebool operator> (const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left> _ Right for pairsreturn (_ Right <_ Left);} // judge whether the value is less than or equal to the function template
Inlinebool operator <= (const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left <= _ Right for pairsreturn (! (_ Right <_ Left);} // determines whether the value is greater than or equal to the template of the function.
Inlinebool operator >=( const pair <_ Ty1, _ Ty2> & _ Left, const pair <_ Ty1, _ Ty2> & _ Right) {// test if _ Left> = _ Right for pairsreturn (! (_ Left <_ Right ));}
Paste a piece of code:
// Pair defines pair.
Pair1; // pair definition and assign a value to pair
Pair2 ("lily", 4); pair
Pair3 (pair2); // pair value assignment method 2 pair1 = make_pair (string ("tom"), 3); // pair value assignment method 3 pair1.first = "jim "; pair1.second = 2; // pair value assignment method four get <0> (pair1) = string ("jim"); get <1> (pair1) = 6; // pair value Assignment Method Five swap (pair1, pair3); // pair Output Method One cout <
<<<
(Pair1) <
<<1>
(Pair1) <
< pre>
<>
<<1>
<<<