C++中sort函數使用方法


一.sort函數

1.sort函數包含在頭文件為#include<algorithm>的c++標准庫中,調用標准庫里的排序方法可以實現對數據的排序,但是sort函數是如何實現的,我們不用考慮!

2.sort函數的模板有三個參數:

void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);

(1)第一個參數first:是要排序的數組的起始地址。

(2)第二個參數last:是結束的地址(最后一個數據的后一個數據的地址)

(3)第三個參數comp是排序的方法:可以是從升序也可是降序。如果第三個參數不寫,則默認的排序方法是從小到大排序。

3.實例

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 main()
 5 {
 6   //sort函數第三個參數采用默認從小到大
 7   int a[]={45,12,34,77,90,11,2,4,5,55};
 8   sort(a,a+10);
 9   for(int i=0;i<10;i++)
10   cout<<a[i]<<" ";     
11 } 

運行結果:

 1 #include<iostream>
 2 #include<algorithm>
 3 using namespace std;
 4 bool cmp(int a,int b);
 5 main(){
 6   //sort函數第三個參數自己定義,實現從大到小 
 7   int a[]={45,12,34,77,90,11,2,4,5,55};
 8   sort(a,a+10,cmp);
 9   for(int i=0;i<10;i++)
10     cout<<a[i]<<" ";     
11 }
12 //自定義函數
13 bool cmp(int a,int b){
14   return a>b;
15 }

運行結果:

 1 #include<iostream>
 2 #include<algorithm>
 3 #include"cstring"
 4 using namespace std;
 5 typedef struct student{
 6   char name[20];
 7   int math;
 8   int english;
 9 }Student;
10 bool cmp(Student a,Student b);
11 main(){
12   //先按math從小到大排序,math相等,按english從大到小排序 
13   Student a[4]={{"apple",67,89},{"limei",90,56},{"apple",90,99}};
14   sort(a,a+3,cmp);
15   for(int i=0;i<3;i++)    
16   cout<<a[i].name <<" "<<a[i].math <<" "<<a[i].english <<endl;     
17 }
18 bool cmp(Student a,Student b){
19   if(a.math >b.math )
20   return a.math <b.math ;//按math從小到大排序 
21   else if(a.math ==b.math )
22       return a.english>b.english ; //math相等,按endlish從大到小排序23 
24 }

運行結果

4.對於容器,容器中的數據類型可以多樣化

  1) 元素自身包含了比較關系,如int,double等基礎類型,可以直接進行比較greater<int>() 遞減, less<int>() 遞增(省略)

 1 #include<iostream>
 2 #include<algorithm>
 3 #include"vector"
 4 using namespace std;
 5 typedef struct student{
 6     char  name[20];
 7     int math;
 8     int english;
 9 }Student;
10 bool cmp(Student a,Student b);
11 main(){
12     int s[]={34,56,11,23,45};
13     vector<int>arr(s,s+5);
14     sort(arr.begin(),arr.end(),greater<int>());
15     for(int i=0;i<arr.size();i++)
16         cout<<arr[i]<<" ";        
17 }

運行結果:

2)元素本身為class或者struct,類內部需要重載< 運算符,實現元素的比較;

 注意事項:bool operator<(const className & rhs) const;  如何參數為引用,需要加const,這樣臨時變量可以賦值;重載operator<為常成員函數,可以被常變量調用; 

 

 1 #include<iostream>
 2 #include<algorithm>
 3 #include"vector"
 4 using namespace std;
 5 typedef struct student{
 6     char  name[20];
 7     int math;
 8     //按math從大到小排序 
 9     inline bool operator < (const student &x) const {
10         return math>x.math ;
11     }
12 }Student;
13 main(){
14     Student a[4]={{"apple",67},{"limei",90},{"apple",90}};
15     sort(a,a+3);
16     for(int i=0;i<3;i++)    
17         cout<<a[i].name <<" "<<a[i].math <<" " <<endl;     
18 }

運行結果:

重載<也可以定義為如下格式:

 

1 struct Cmp{
2     bool operator()(Info a1,Info a2) const {
3     return a1.val > a2.val;
4     }
5 };

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM