C ++ programming-) pair (peer group)

Source: Internet
Author: User

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> <<<

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.