In C + +, the structure vector uses sort sort __c++

Source: Internet
Author: User
first, encountered problems:

Writing code today is a question of how to sort vectors, vaguely remembering that the Std::sort function can sort vectors, but this time the vector that needs to be sorted is defined by its own structure (the element is greater than or equal to 2), You cannot use the sort function directly if you want to sort by either positive or reverse order of one of these elements.


Second, the solution:

In 1.c++ when the data type in the vector is the base type, we call the Std::sort function to easily sort the ascending and descending order of the data members in the vector, as shown in the following code (excerpt from http://www.cplusplus.com/reference/ algorithm/sort/):

[cpp]  View plain  copy// sort algorithm example   #include  <iostream >     // std::cout   #include  <algorithm>     // std::sort   #include  <vector>        // std::vector      bool myfunction  (int i,int j)  {  return  (i<j); }      struct myclass {      Bool operator ()   (int i,int j)  { return  (i<j);}   } myobject;      int main  ()  {     int  myints[] = {32,71,12,45,26,80,53,33};     std::vector<int>  myvector  (myints, myints+8);                // 32 71 12 45 26 80 53 33        // using default  comparison  (operator <):     std::sort  (Myvector.begin (),  Myvector.begin () +4);            //(12 32 45  71) 26 80 53 33        // using function as  comp     std::sort  (Myvector.begin () +4, myvector.end (),  myfunction);  // 12 32 45 71 (26 33 53 80)         //  using object as comp     std::sort  (Myvector.begin (),  Myvector.end (),  myobject);      //(12 26 32 33 45 53  71 80)         // print out content:      std::cout <<  "Myvector contains: ";     for  (std::vector<int>::iterator it= Myvector.begin ()  it!=myvector.end ();  ++it)        std::cout < <  '   '  << *it;     std::cout <<  ' \ n ';         return 0;  }  


The output is:

[CPP] View plain copy myvector Contains:12 26 32 33 45 53 71 80

2. However, when the data type in the vector is a custom struct type, how do we implement the sort.
In fact, the above code in the Std::sort function of the third parameter comp called function or object to modify it. Here we use the function as an example of comp, the code is as follows:

[CPP] View plain copy #include <vector> #include <iostream> #include <algorithm> using NAMESP      Ace STD; struct Point2 {int x;

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.