About using the Sort method for vector and set in C + +

Source: Internet
Author: User
Tags prev

Vector and set are very handy containers in C + +.

The sort method is a standard function in the algorithm header file that can be efficiently sorted by default, by element from small to large

Use the Sort method in vector and set to achieve a variety of sorts that suit your needs

First the Sort method can sort the static array

1#include <iostream>2 using namespacestd;3 intMain () {4     inta[Ten] = {9,0,1,2,3,7,4,5, -,Ten };5Sort (A, A +Ten);6      for(inti =0; I <Ten; i++)7cout << A[i] <<Endl;8     return 0;9}

Operation Result:

As you can see here is sort (a,a+10), but there are only 9 elements in array A, why a+10 instead of a+9?

Because the sort method actually corresponds to the last address of the number is not taken,

and vector,set,map the value of the end () of these containers is not actually the last value, but the last value of end!

You need to use Prev (Xxx.end ()) to remove the last element in the container.

Use the sort function for vectors:

The first scenario: basic types, such as vector<int>,vector<double>,vector<string>, are also possible

1#include <iostream>2#include <vector>3#include <algorithm>4 using namespacestd;5 intMain () {6vector<int>A;7     intn =5;8      while(n--){9         intscore;TenCIN >>score; One A.push_back (score); A     } -     //cout << "A.end ()" << *a.end () << Endl; Execution of this sentence will be an error!  -cout <<"prev (a.end)"<< *prev (A.end ()) <<Endl; the sort (A.begin (), A.end ()); -      for(vector<int>::iterator it = A.begin (); It! = A.end (); it++){ -cout << *it <<Endl; -     } +     return 0; -}

Execution Result:

See, actually the last pointer to the end of the point is the element that was inserted at the end of the value!

Sorted after a small big.

The second scenario: using a custom structure for the sort algorithm,

At this point you need to define a comparison function, because the sort algorithm is based on the elements in the container can be 22 compared, and then from small to large sort, so to customize what is less than (' < ')

1#include <iostream>2#include <vector>3#include <Set>4#include <string>5#include <algorithm>6 using namespacestd;7 structstudent{8     Charname[Ten];9     intscore;Ten }; One //custom "less than" A BOOLCompConstStudent &a,ConstStudent &b) { -     returnA.score <B.score; - } the intMain () { -Vector<student>vectorstudents; -     intn =5; -      while(n--){ + student onestudent; -         stringname; +         intscore; ACIN >> Name >>score; at strcpy (Onestudent.name, Name.c_str ()); -Onestudent.score =score; - Vectorstudents.push_back (onestudent); -     } -cout <<"=========== sort before ================"<<Endl; -      for(Vector<student>::iterator it = Vectorstudents.begin (); It! = Vectorstudents.end (); it++){ incout <<"Name:"<< It->name <<"Score:"<< It->score <<Endl; -     } to sort (Vectorstudents.begin (), Vectorstudents.end (), comp); +cout <<"=========== sort after ================"<<Endl; -      for(Vector<student>::iterator it = Vectorstudents.begin (); It! = Vectorstudents.end (); it++){ thecout <<"Name:"<< It->name <<"Score:"<< It->score <<Endl; *     } $     return 0;Panax Notoginseng}

Operation Result:

Next, do a similar operation for set.

Set is a collection, the inner element does not repeat, and it is automatically sorted, also from small to large

And the Set Insert method does not have an insert (A,CMP) overload, so if you want to insert the struct into the set, we'll overload the ' < ' operator.

The Set method is also small to large at the time of insertion, so let's reload the < operator to sort it from large to small

1#include <iostream>2#include <vector>3#include <Set>4#include <string>5#include <algorithm>6 using namespacestd;7 structstudent{8     Charname[Ten];9     intscore;Ten }; One //custom "less than" A BOOLCompConstStudent &a,ConstStudent &b) { -     returnA.score <B.score; - } the BOOL operator< (ConstStudent & STU1,ConstStudent &STU2) { -     returnStu1.score >Stu2.score; - } - intMain () { +     //vector<student> vectorstudents; -     Set<student>setstudents; +     //int n = 5; A     intn =6; at      while(n--){ - student onestudent; -         stringname; -         intscore; -CIN >> Name >>score; - strcpy (Onestudent.name, Name.c_str ()); inOnestudent.score =score; - Setstudents.insert (onestudent); to     } +cout <<"=========== sort before ================"<<Endl; -      for(Set<student>::iterator it = Setstudents.begin (); It! = Setstudents.end (); it++){ thecout <<"Name:"<< It->name <<"Score:"<< It->score <<Endl; *     } $     //sort (Setstudents.begin (), Setstudents.end (), comp);Panax Notoginseng     //cout << "=========== ================" << Endl; -     //For (Set<student>::iterator it = Setstudents.begin (); It! = Setstudents.end (); it++) { the     //cout << "Name:" << it->name << "score:" << it->score << Endl; +     //} A     return 0; the}

Operation Result:

As we can see, the elements within the set are not duplicated, and it is sorted according to what it considers to be "small to large"

About vector and set in C + + using the Sort method

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.