Vector sorting in C ++

Source: Internet
Author: User

Vector sorting in C ++
When sorting by vector, it is found that there is a problem with the sort () function. sort sorting only returns the sorted results and does not return the original numbers of the results of each sort.

// Template
 
  
Bool cmp (const pair
  
   
& Amp; x, const pair
   
    
& Y) {return x. second> y. second;} // template
    
     
Void sortVector (vector
     
      
& Value, vector
      
        & Lable) {lable. clear (); // defines an array of key-value pairs. The first Int is the key, and the second Int Is the value vector.
       
         > TheMap; for (vector
        
          : Size_type I = 0; I
         
           >:: Size_type I = 0; I
          
           

The above code can solve this problem. The Calling method of the above Code is as follows:

 

Vector
            
             
A; a. push_back (1); a. push_back (2); a. push_back (4); a. push_back (3); vector
             
              
Lable; sortVector (a, lable); cout <
              
               
The principle is actually very simple. It is to combine data and numbers into a key-value pair, then define these key-value pairs as a vector <> variable, and then sort the vector with the sort function.
               

 

 

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.