c++入门必学库函数sort的基本用法

一、sort 的介绍

sort是c++ algorithm 库里的一个排序函数。排序太常用了,如果每次都要自己写排序函数的话会浪费程序员大量的时间和精力,所以函数库里就写好了一些排序算法以供我们使用。

sort()是不稳定的排序,底层使用的是快速排序算法,平均时间复杂度为O(n*log n)

如果需要稳定排序可以使用stable_sort(),底层使用归并排序实现的,时间复杂度固定是O(n*log n)

sort()和stable_sort()用法是一样的,下面我们只讲解sort()的使用

二、sort的基本用法

sort(起始地址,末尾地址+1);

sort(起始地址,末尾地址+1,cmp);

sort是默认升序排序的,如果需要自定义排序,可以写一个比较函数,用第二种方法排序

1、普通数组的排序

示例代码:

#include<iostream>
#include<algorithm> //使用sort等算法函数需要的头文件 
using namespace std;
void print(int a[]){//打印函数 
        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 9

sort(a,a+10,cmp)自定义降序排序后:

9 8 7 6 5 4 3 2 1 0

2、结构体的排序

因为结构体默认是没有比较大小的功能的,所以我们必须使用cmp函数定义排序方法

示例代码:

#include<iostream>
#include<algorithm> //使用sort等算法函数需要的头文件 
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等数据结构的排序

像vector和string等数据结构,我们排序时是不能像数组那样直接用名字代表地址来排序的,而必须使用它们的迭代器begin()和end()来完成排序

示例代码:

#include<iostream>
#include<vector> //使用vector容器需要使用这个头文件 
#include<algorithm> //使用sort等算法函数需要的头文件 
using namespace std;

print(vector<int> v){//打印函数 
        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());//反向迭代器可实现降序排序 
        cout<<"sort(v.rbegin(),v.rend())降序排序后:"<<endl; 
        print(v);
} 

运行结果:

排序前:

2 3 7 1 9 8 0 5 4

sort(v.begin(),v.end())升序排序后:

0 1 2 3 4 5 7 8 9

sort(v.rbegin(),v.rend())降序排序后:

9 8 7 5 4 3 2 1 0

当然,vector等结构都是可以用cmp函数自定义排序方法的,感兴趣的同学可以尝试一下,这里就不在叙述了,因为都是大同小异的。学会举一反三学习效率才会高

总结

原文地址:https://blog.csdn.net/weixin_52115456/article/details/127613394