關於C++中vector和set使用sort方法進行排序

C++中vector和set都是很是方便的容器,ios

sort方法是algorithm頭文件裏的一個標準函數,能進行高效的排序,默認是按元素從小到大排序算法

將sort方法用到vector和set中能實現多種符合本身需求的排序數組

首先sort方法能夠對靜態的數組進行排序函數

1 #include<iostream>
2 using namespace std;
3 int main(){
4     int a[10] = { 9, 0, 1, 2, 3, 7, 4, 5, 100, 10 };
5     sort(a, a +10);
6     for (int i = 0; i < 10; i++)
7         cout << a[i] << endl;
8     return 0;
9 }

運行結果:spa

 

這裏能夠看到是sort(a,a+10),可是數組a一共只有9個元素,爲何是a+10而不是a+9呢?指針

由於sort方法實際上最後一位地址對應的數是不取的,code

並且vector,set,map這些容器的end()取出來的值實際上並非最後一個值,而end的前一個纔是最後一個值!blog

須要用prev(xxx.end()),才能取出容器中最後一個元素。排序

 

對vector使用sort函數:ci

第一種情形:基本類型,如vector<int>,vector<double>,vector<string>也是能夠的

 1 #include<iostream>
 2 #include<vector>
 3 #include<algorithm>
 4 using namespace std;
 5 int main(){
 6     vector<int> a;
 7     int n = 5;
 8     while (n--){
 9         int score;
10         cin >> score;
11         a.push_back(score);
12     }
13     //cout <<" a.end()"<< *a.end() << endl;       執行這句話會報錯!
14     cout << " prev(a.end)" << *prev(a.end()) << endl;
15     sort(a.begin(), a.end());
16     for (vector<int>::iterator it = a.begin(); it != a.end(); it++){
17         cout << *it << endl;
18     }
19     return 0;
20 }

執行結果:

看到了嗎,實際上end的前一個指針指向的元素纔是插入時的最後一個值!

排序後從小大大。

第二種情形:用自定義的結構體進行sort算法,

這時候須要本身定義個比較函數,由於sort算法是基於容器中的元素是能夠兩兩比較的,而後從小到大排序,因此要自定義怎麼樣纔是小於('<')

 1 #include<iostream>
 2 #include<vector>
 3 #include<set>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 struct student{
 8     char name[10];
 9     int score;
10 };
11 //自定義「小於」
12 bool comp(const student &a, const student &b){
13     return a.score < b.score;
14 }
15 int main(){
16     vector<student> vectorStudents;
17     int n = 5;
18     while (n--){
19         student oneStudent;
20         string name;
21         int score;
22         cin >> name >> score;
23         strcpy(oneStudent.name, name.c_str());
24         oneStudent.score = score;
25         vectorStudents.push_back(oneStudent);
26     }
27     cout << "===========排序前================" << endl;
28     for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
29         cout << "name: " << it->name << " score: " << it->score << endl;
30     }
31     sort(vectorStudents.begin(),vectorStudents.end(),comp);
32     cout << "===========排序後================" << endl;
33     for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
34         cout << "name: " << it->name << " score: " << it->score << endl;
35     }
36     return 0;
37 }

運行結果:

 

不過有時候一個排序條件不夠,好比要求學生按分數從高到低排序,若是分數相同,則按照年齡從大到小排序

就須要在comp自定義函數裏面修改一下判斷了,原來是直接return a.score < b.score

如今就須要判斷

if (a.score > b.score)
   return true; else if (a.score == b.score && a.age > b.age)
return true; else return false;

這裏必定要記得else return false!!!有一次比賽的時候寫到這個函數,有三個判斷條件,結果忘了這茬,老是報錯,
到後來有點着急了就本身手動實現了一下寫了三個比較函數,調用了三次sort函數!!!!!
 1 #include<iostream>
 2 #include<vector>
 3 #include<set>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 struct student{
 8     char name[10];
 9     int score;
10     int age;
11 };
12 //自定義「小於」
13 bool comp(const student &a, const student &b){
14     if (a.score > b.score)
15         return true;
16     else if (a.score == b.score  && a.age > b.age)
17         return true;
18     else                ///這裏的else return false很是重要!!!!!
19         return false;
20 }
21 int main(){
22     vector<student> vectorStudents;
23     /*set<student> setStudents;*/
24     //int n = 5;
25     int n = 6;
26     while (n--){
27         student oneStudent;
28         string name;
29         int score;
30         int age;
31         cin >> name >> score>>age;
32         strcpy(oneStudent.name, name.c_str());
33         oneStudent.score = score;
34         oneStudent.age = age;
35         vectorStudents.push_back(oneStudent);
36     }
37     cout << "===========排序前================" << endl;
38     for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
39         cout << "name: " << it->name << " score: " << it->score << " age: "<<it->age<<endl;
40     }
41     sort(vectorStudents.begin(), vectorStudents.end(), comp);
42     //sort(setStudents.begin(), setStudents.end());
43     cout << "===========排序後================" << endl;
44     for (vector<student>::iterator it = vectorStudents.begin(); it != vectorStudents.end(); it++){
45         cout << "name: " << it->name << " score: " << it->score << " age: " << it->age << endl;
46     }
47     return 0;
48 }

運行結果以下:

接下來,對於set作相似的操做。

set是一個集合,內部的元素不會重複,同時它會自動進行排序,也是從小到大

並且set的insert方法沒有insert(a,cmp)這種重載,因此若是要把結構體插入set中,咱們就要重載'<'運算符。

set方法在插入的時候也是從小到大的,那麼咱們重載一下<運算符讓它從大到小排序

 1 #include<iostream>
 2 #include<vector>
 3 #include<set>
 4 #include<string>
 5 #include<algorithm>
 6 using namespace std;
 7 struct student{
 8     char name[10];
 9     int score;
10 };
11 //自定義「小於」
12 bool comp(const student &a, const student &b){
13     return a.score < b.score;
14 }
15 bool operator < (const student & stu1,const student &stu2){
16     return stu1.score > stu2.score;
17 }
18 int main(){
19     //vector<student> vectorStudents;
20     set<student> setStudents;
21     //int n = 5;
22     int n = 6;
23     while (n--){
24         student oneStudent;
25         string name;
26         int score;
27         cin >> name >> score;
28         strcpy(oneStudent.name, name.c_str());
29         oneStudent.score = score;
30         setStudents.insert(oneStudent);
31     }
32     cout << "===========排序前================" << endl;
33     for (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++){
34         cout << "name: " << it->name << " score: " << it->score << endl;
35     }
36     //sort(setStudents.begin(), setStudents.end(), comp);
37     //cout << "===========排序後================" << endl;
38     //for (set<student>::iterator it = setStudents.begin(); it != setStudents.end(); it++){
39     //    cout << "name: " << it->name << " score: " << it->score << endl;
40     //}
41     return 0;
42 }

運行結果:

咱們能夠看到,set內元素不會重複,並且它按照它所認爲的「從小到大」進行了排序

相關文章
相關標籤/搜索