c++入門(mén)必學(xué)庫(kù)函數(shù)sort的基本用法
一、sort 的介紹
sort是c++ algorithm 庫(kù)里的一個(gè)排序函數(shù)。排序太常用了,如果每次都要自己寫(xiě)排序函數(shù)的話(huà)會(huì)浪費(fèi)程序員大量的時(shí)間和精力,所以函數(shù)庫(kù)里就寫(xiě)好了一些排序算法以供我們使用。
sort()是不穩(wěn)定的排序,底層使用的是快速排序算法,平均時(shí)間復(fù)雜度為O(n*log n)
如果需要穩(wěn)定排序可以使用stable_sort(),底層使用歸并排序?qū)崿F(xiàn)的,時(shí)間復(fù)雜度固定是O(n*log n)
sort()和stable_sort()用法是一樣的,下面我們只講解sort()的使用
二、sort的基本用法
sort(起始地址,末尾地址+1);
sort(起始地址,末尾地址+1,cmp);
sort是默認(rèn)升序排序的,如果需要自定義排序,可以寫(xiě)一個(gè)比較函數(shù),用第二種方法排序
1、普通數(shù)組的排序
示例代碼:
#include<iostream> #include<algorithm> //使用sort等算法函數(shù)需要的頭文件 using namespace std; void print(int a[]){//打印函數(shù) for(int i=0;i<10;i++){ cout<<a[i]<<' '; } cout<<endl; } bool cmp(int a1,int a2){//大于號(hào)是升序排序,小于號(hào)是降序排序 return a1>a2; } int main(){ int a[10]={3,1,4,5,8,0,9,2,7,6}; cout<<"排序前:"<<endl; print(a); //打印 cout<<endl; sort(a,a+10);//排序,默認(rèn)是升序的 cout<<"sort(a,a+10)排序后:"<<endl; print(a); //打印 cout<<endl; sort(a,a+10,cmp);//自定義排序 cout<<"sort(a,a+10,cmp)自定義降序排序后:"<<endl; print(a); }
運(yùn)行結(jié)果:
排序前:
3 1 4 5 8 0 9 2 7 6
sort(a,a+10)排序后:
0 1 2 3 4 5 6 7 8 9sort(a,a+10,cmp)自定義降序排序后:
9 8 7 6 5 4 3 2 1 0
2、結(jié)構(gòu)體的排序
因?yàn)榻Y(jié)構(gòu)體默認(rèn)是沒(méi)有比較大小的功能的,所以我們必須使用cmp函數(shù)定義排序方法
示例代碼:
#include<iostream> #include<algorithm> //使用sort等算法函數(shù)需要的頭文件 using namespace std; struct test{ int a; int b; }; bool cmp1(test t1,test t2){//先按a升序排序,再按b升序排序 if(t1.a==t2.a){ return t1.b<t2.b; } return t1.a<t2.a; } bool cmp2(test t1,test t2){//先按a降序排序,再按b降序排序 if(t1.a==t2.a){ return t1.b>t2.b; } return t1.a>t2.a; } void print(test t[]){ for(int i=0;i<5;i++){ cout<<"t["<<i<<"]("<<t[i].a<<","<<t[i].b<<") "; } cout<<endl; } int main(){ test t[5]; t[0].a=2; t[0].b=3; t[1].a=5; t[1].b=3; t[2].a=5; t[2].b=2; t[3].a=2; t[3].b=8; t[4].a=1; t[4].b=1; cout<<"排序前:"<<endl; print(t); //打印 cout<<endl; sort(t,t+5,cmp1);//自定義升序排序 cout<<"sort(a,a+5,cmp1)自定義升序排序后:"<<endl; print(t); cout<<endl; sort(t,t+5,cmp2);//自定義降序排序 cout<<"sort(a,a+5,cmp2)自定義降序序排序后:"<<endl; print(t); }
運(yùn)行結(jié)果:
排序前:
t[0](2,3) t[1](5,3) t[2](5,2) t[3](2,8) t[4](1,1)sort(a,a+5,cmp1)自定義升序排序后:
t[0](1,1) t[1](2,3) t[2](2,8) t[3](5,2) t[4](5,3)sort(a,a+5,cmp2)自定義降序序排序后:
t[0](5,3) t[1](5,2) t[2](2,8) t[3](2,3) t[4](1,1)
3、vector等數(shù)據(jù)結(jié)構(gòu)的排序
像vector和string等數(shù)據(jù)結(jié)構(gòu),我們排序時(shí)是不能像數(shù)組那樣直接用名字代表地址來(lái)排序的,而必須使用它們的迭代器begin()和end()來(lái)完成排序
示例代碼:
#include<iostream> #include<vector> //使用vector容器需要使用這個(gè)頭文件 #include<algorithm> //使用sort等算法函數(shù)需要的頭文件 using namespace std; print(vector<int> v){//打印函數(shù) for(int i=0;i<v.size();i++){ cout<<v[i]<<' '; } cout<<endl; } int main(){ vector<int> v;//定義一個(gè)int型的vector v.push_back(2);//在尾部插入一個(gè)元素2 v.push_back(3);//在尾部插入一個(gè)元素3 v.push_back(7); v.push_back(1); v.push_back(9); v.push_back(8); v.push_back(0); v.push_back(5); v.push_back(4); cout<<"排序前:"<<endl; print(v); cout<<endl; sort(v.begin(),v.end());//用迭代器排序 cout<<"sort(v.begin(),v.end())升序排序后:"<<endl; print(v); cout<<endl; sort(v.rbegin(),v.rend());//反向迭代器可實(shí)現(xiàn)降序排序 cout<<"sort(v.rbegin(),v.rend())降序排序后:"<<endl; print(v); }
運(yùn)行結(jié)果:
排序前:
2 3 7 1 9 8 0 5 4sort(v.begin(),v.end())升序排序后:
0 1 2 3 4 5 7 8 9sort(v.rbegin(),v.rend())降序排序后:
9 8 7 5 4 3 2 1 0
當(dāng)然,vector等結(jié)構(gòu)都是可以用cmp函數(shù)自定義排序方法的,感興趣的同學(xué)可以嘗試一下,這里就不在敘述了,因?yàn)槎际谴笸‘惖?。學(xué)會(huì)舉一反三學(xué)習(xí)效率才會(huì)高
總結(jié)
到此這篇關(guān)于c++入門(mén)必學(xué)庫(kù)函數(shù)sort基本用法的文章就介紹到這了,更多相關(guān)c++庫(kù)函數(shù)sort內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)矩陣翻轉(zhuǎn)(上下翻轉(zhuǎn)、左右翻轉(zhuǎn))
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)矩陣翻轉(zhuǎn)(上下翻轉(zhuǎn)、左右翻轉(zhuǎn))的相關(guān)資料,需要的朋友可以參考下2017-05-05C語(yǔ)言實(shí)現(xiàn)猜數(shù)字小游戲的示例代碼
猜數(shù)字小游戲是我們小時(shí)候喜歡我們一個(gè)經(jīng)典小游戲。這篇文章將利用C語(yǔ)言中的循環(huán)語(yǔ)句、分支語(yǔ)句和函數(shù)實(shí)現(xiàn)這一游戲,需要的可以參考一下2022-10-10C語(yǔ)言數(shù)據(jù)結(jié)構(gòu)之線性表的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)
線性表是最基本、最簡(jiǎn)單、也是最常用的一種數(shù)據(jù)結(jié)構(gòu)。線性表(linear list)是數(shù)據(jù)結(jié)構(gòu)的一種,一個(gè)線性表是n個(gè)具有相同特性的數(shù)據(jù)元素的有限序列,這篇文章帶你學(xué)習(xí)下線性表的鏈?zhǔn)酱鎯?chǔ)結(jié)構(gòu)2021-11-11