当前位置 博文首页 > 文章内容

    C++中sort()函数使用方法

    作者: 栏目:未分类 时间:2020-09-30 11:00:17

    本站于2023年9月4日。收到“大连君*****咨询有限公司”通知
    说我们IIS7站长博客,有一篇博文用了他们的图片。
    要求我们给他们一张图片6000元。要不然法院告我们

    为避免不必要的麻烦,IIS7站长博客,全站内容图片下架、并积极应诉
    博文内容全部不再显示,请需要相关资讯的站长朋友到必应搜索。谢谢!

    另祝:版权碰瓷诈骗团伙,早日弃暗投明。

    相关新闻:借版权之名、行诈骗之实,周某因犯诈骗罪被判处有期徒刑十一年六个月

    叹!百花齐放的时代,渐行渐远!



    一.sort函数

    1.sort函数包含在头文件为#include<algorithm>的c++标准库中,调用标准库里的排序方法可以实现对数据的排序,但是sort函数是如何实现的,我们不用考虑!

    2.sort函数的模板有三个参数:

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

    (1)第一个参数first:是要排序的数组的起始地址。

    (2)第二个参数last:是结束的地址(最后一个数据的后一个数据的地址)

    (3)第三个参数comp是排序的方法:可以是从升序也可是降序。如果第三个参数不写,则默认的排序方法是从小到大排序。

    3.实例

    #include<iostream>
    #include<algorithm>
    using namespace std;
    main()
    {
      //sort函数第三个参数采用默认从小到大
      int a[]={45,12,34,77,90,11,2,4,5,55};
      sort(a,a+10);
      for(int i=0;i<10;i++)
      cout<<a[i]<<" ";   
    }

    运行结果:

     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;
    
    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 };

    参考:https://www.cnblogs.com/junbaobei/p/10776066.html