c++入門必學庫函數(shù)sort的基本用法
一、sort 的介紹
sort是c++ algorithm 庫里的一個排序函數(shù)。排序太常用了,如果每次都要自己寫排序函數(shù)的話會浪費程序員大量的時間和精力,所以函數(shù)庫里就寫好了一些排序算法以供我們使用。
sort()是不穩(wěn)定的排序,底層使用的是快速排序算法,平均時間復雜度為O(n*log n)
如果需要穩(wěn)定排序可以使用stable_sort(),底層使用歸并排序實現(xiàn)的,時間復雜度固定是O(n*log n)
sort()和stable_sort()用法是一樣的,下面我們只講解sort()的使用
二、sort的基本用法
sort(起始地址,末尾地址+1);
sort(起始地址,末尾地址+1,cmp);
sort是默認升序排序的,如果需要自定義排序,可以寫一個比較函數(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){//大于號是升序排序,小于號是降序排序
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);//排序,默認是升序的
cout<<"sort(a,a+10)排序后:"<<endl;
print(a); //打印
cout<<endl;
sort(a,a+10,cmp);//自定義排序
cout<<"sort(a,a+10,cmp)自定義降序排序后:"<<endl;
print(a);
}
運行結果:
排序前:
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、結構體的排序
因為結構體默認是沒有比較大小的功能的,所以我們必須使用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);
}
運行結果:
排序前:
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ù)結構的排序
像vector和string等數(shù)據(jù)結構,我們排序時是不能像數(shù)組那樣直接用名字代表地址來排序的,而必須使用它們的迭代器begin()和end()來完成排序
示例代碼:
#include<iostream>
#include<vector> //使用vector容器需要使用這個頭文件
#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;//定義一個int型的vector
v.push_back(2);//在尾部插入一個元素2
v.push_back(3);//在尾部插入一個元素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());//反向迭代器可實現(xiàn)降序排序
cout<<"sort(v.rbegin(),v.rend())降序排序后:"<<endl;
print(v);
}
運行結果:
排序前:
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
當然,vector等結構都是可以用cmp函數(shù)自定義排序方法的,感興趣的同學可以嘗試一下,這里就不在敘述了,因為都是大同小異的。學會舉一反三學習效率才會高
總結
到此這篇關于c++入門必學庫函數(shù)sort基本用法的文章就介紹到這了,更多相關c++庫函數(shù)sort內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
VisualStudio2022下配置 OpenMP多線程編程環(huán)境與運行
本文主要介紹了VisualStudio2022下配置 OpenMP多線程編程環(huán)境與運行,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2024-06-06
Qt使用SQLite數(shù)據(jù)庫存儲管理圖片文件
這篇文章主要為大家詳細介紹了Qt如何使用SQLite數(shù)據(jù)庫實現(xiàn)存儲管理圖片文件的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2023-04-04

