Sort () sort of vectors in C + + sorting vector structure parts

Source: Internet
Author: User
Tags rand
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
BOOL Comp (const int &a,const int &b)              //Sort () comparison
{return
    a>b;
}
int main (int argc, const char * argv[]) {
    //int a[11]={9,8,7,6,5,4,3,2,1,10};
    vector<int>a;
    for (int i=1; i<=50; i++) {
        a.push_back (i);                        Assign a value to a
        //a.push_back (rand ());                 RAND () generates random number
    }
    random_shuffle (A.begin (), A.end ());        Randomly upset a
    sort (a.begin (), A.begin () +50);           Sort ()
    for (int i=0; i<=49; i++) {printf ("%d", a[i]); printf ("\ n");
    return 0;
}

The sort function works very well in use and is very simple, and is not an order of magnitude for efficiency and bubbling or selection sorting. In this article, the use of the sort function in vectors is divided into the starting usages of the sort function and the custom comp comparison function to compare the two basic functions of the structure body:

1, sort Getting started:

Use sort to include the algorithm header file, complete with the following code

#include <iostream>
#include <vector>
#include <algorithm>//seems to be OK, but it's best to add.
using namespace std;
int main ()
{
    vector<int>v;
    V.push_back (a);
    V.push_back (a);
    V.push_back ();
    V.push_back (233);
    V.push_back (113);
    Sort (V.begin (), V.end ());
    int i=0;
    for (i=0;i<5;i++)
    {
        cout<<v[i]<<endl;
    }
    System ("pause");
    return 0;
}

The results of the operation are as follows:

3
13
23
113
233
Please press any key to continue ...

You can see the result is from small to large sort, but if I need to sort from big to small.

2, rewrite comp from big to small sort.

After adding the comp function, the code is as follows:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
BOOL Comp (const int &a,const int &b)
{return
    a>b;
}
int main ()
{
    vector<int>v;
    V.push_back (a);
    V.push_back (a);
    V.push_back ();
    V.push_back (233);
    V.push_back (113);
    Sort (V.begin (), V.end (), comp);
    int i=0;
    for (i=0;i<5;i++)
    {
        cout<<v[i]<<endl;
    }
    System ("pause");
    return 0;
}

Run Result:

233
113
23
13
3
Please press any key to continue ...

Why is that so? In comparison, the sort function is based on the comp function to determine the size of the output, the system default A<b return True, so from small to large row, and my comp function is set to A>b when returned as true, then the final result of the sort results are correspondingly from small to large. It's simple ~ ~

3. Sort the structure

With the comp function, we can sort any object in any structure, and we just need to modify the comp function to realize it. The code is as follows:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct SS
{
    int a,b;
};
BOOL Comp (const SS &a,const SS &b)
{return
    a.a<b.a;
}
int main ()
{
    vector<ss>v;
    SS S1,s2,s3,s4,s5;
    s1.a=4;s1.b=23;
    s2.a=1;s2.b=213;
    s3.a=2;s3.b=231;
    s4.a=5;s4.b=123;
    s5.a=3;s5.b=223;
    V.push_back (S1);
    V.push_back (S2);
    V.push_back (S3);
    V.push_back (S4);
    V.push_back (S5);
    Sort (V.begin (), V.end (), comp);
    int i=0;
    for (i=0;i<5;i++)
    {
        cout<<v[i].a<< "" <<v[i].b<<endl;
    }
    System ("pause");
    return 0;
}

For example, the SS structure of A is the index number, B represents the index corresponding to the value, then I want to sort by index, by rewriting the comp function can be implemented.

Results:

1 213
2 231
3 223
4 23
5 123
Please press any key to continue ...



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.