C++中sort函数的用法

C++中有很多好用的库函数

用起来方便又快捷

最喜欢sort这个函数

但是经常记混它的用法

在此总结一下

方便学习

 

       Sort()函数是C++一种排序方法之一,学会了这种方法也打消我学习C++以来使用的冒泡排序和选择排序所带来的执行效率不高的问题!因为它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n),执行效率较高。

      (1)Sort()函数的头文件为#include<algorithm>    

      (2)Sort函数有三个参数:

第一个是要排序的数组的起始地址。

第二个是结束地址(最后一位要排序的地址)

第三个参数是排序的方法,可以从小到大也可以是从大到小,当不写第三个参数时默认的排序方法时从小到大排序。

sort 数值降序排序代码:

[cpp] view plain copy

  1. <span style="font-size:18px;">#include<stdio.h>  
  2. #include<algorithm>  
  3. using namespace std;  
  4. bool cmp(int a,int b)  
  5. {  
  6.     return a>b;  
  7. }  
  8. int main()  
  9. {  
  10.     int a[10]={2,5,3,0,1,5,6,2,8,4};  
  11.     printf("排序前:");  
  12.     for(int i=0;i<10;i++)  
  13.         printf("%d ",a[i]);  
  14.     sort(a,a+10,cmp);  
  15.     printf("\n");  
  16.     printf("排序后:");  
  17.     for(int i=0;i<10;i++)  
  18.         printf("%d ",a[i]);  
  19.     printf("\n");  
  20.     return 0;  
  21. }</span>  

当然,如果不想写函数,C++标准库的强大功能完全可以解决这个问题。

less<数据类型>()  //升序排序

greater<数据类型>()  //降序排序

例如:sort(a,a+10,less<int>());

   sort(a,a+10,greater<int>());

sort函数也可以实现根据ASCII码值对字符进行排序,只需把数据类型改成char就可以了。

sort ASCII升序排序代码:

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<algorithm>
using namespace std;
bool cmp(char a,char b)
{
    return a<b;
}
int main()
{char s[10];
int i;
   while(cin>>s)
   {
       for(i=0;i<3;i++)
        sort(s,s+3,cmp);
        for(i=0;i<2;i++)
            cout<<s[i]<<' ';
        cout<<s[2]<<endl;
   }
}

sort函数对结构体进行排序

[cpp] view plain copy

  1. <span style="font-size:18px;">#include<stdio.h>  
  2. #include<algorithm>  
  3. using namespace std;  
  4. struct student  
  5. {  
  6.     int grade;  
  7.     int num_id;  
  8. }stu[10];  
  9. bool cmp(struct student a,struct student b)  
  10. {  
  11.     if(a.grade!=b.grade)  
  12.         return a.grade>b.grade;      //若成绩不相等,按成绩降序   
  13.     else  
  14.         return a.num_id>b.num_id;    //若成绩相等,按学号降序   
  15. }  
  16. int main()  
  17. {  
  18.     for(int i=0;i<10;i++)  
  19.         scanf("%d %d",&stu[i].grade,&stu[i].num_id);  
  20.     sort(stu,stu+10,cmp);  
  21.     printf("成绩\t\t学号\n");  
  22.     for(int i=0;i<10;i++)  
  23.         printf("%d\t\t%d\n",stu[i].grade,stu[i].num_id);  
  24.     return 0;  
  25. }</span>  

 

如果不写函数的话使用sort函数对pair数组排序,默认按first进行升序排序

当然想按其他对某个值进行排序的话就得写cmp函数了

在格式上跟上面的并没有很大的区别

注意参数的写法就可以了

sort函数对pair数组进行排序如:

[cpp] view plain copy

  1. typedef pair<int,int> P;  

[cpp] view plain copy

  1. bool cmp(const P &a, const P &b)   //P为pair数组  
  2. {  
  3.     if (a.first < b.first)  
  4.         return true;  
  5.     else return false;  
  6. }</span>  

sort函数对字符数组进行排序:

杭电1862  EXCEL排序  http://acm.hdu.edu.cn/showproblem.php?pid=1862

[cpp] view plain copy

  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<algorithm>  
  4. using namespace std;  
  5. struct student  
  6. {  
  7.     char stuID[10];  
  8.     char name[10];  
  9.     int grade;  
  10. }stu[100005];  
  11. bool cmp1(struct student a,struct student b)  
  12. {  
  13.     return strcmp(a.stuID,b.stuID)<0;  
  14. }  
  15. bool cmp2(struct student a,struct student b)  
  16. {  
  17.     if(strcmp(a.name,b.name)==0)  
  18.         return strcmp(a.stuID,b.stuID)<0;  
  19.     else  
  20.         return strcmp(a.name,b.name)<0;  
  21. }  
  22. bool cmp3(struct student a,struct student b)  
  23. {  
  24.     if(a.grade==b.grade)  
  25.         return strcmp(a.stuID,b.stuID)<0;  
  26.     else  
  27.         return a.grade<b.grade;  
  28. }  
  29. int main()  
  30. {  
  31.     int i,n,c,k=0;  
  32.     while(scanf("%d %d",&n,&c),n,c)  
  33.     {  
  34.         for(i=0;i<n;i++)  
  35.             scanf("%s %s %d",&stu[i].stuID,stu[i].name,&stu[i].grade);  
  36.         if(c==1)  
  37.             sort(stu,stu+n,cmp1);  
  38.         else if(c==2)  
  39.             sort(stu,stu+n,cmp2);  
  40.         else if(c==3)  
  41.             sort(stu,stu+n,cmp3);  
  42.         printf("Case %d:\n",++k);  
  43.         for(i=0;i<n;i++)  
  44.             printf("%s %s %d\n",stu[i].stuID,stu[i].name,stu[i].grade);  
  45.     }  
  46.     return 0;  
  47. }  

 

 


对于cmp函数的写法没有什么固定的模板,需要根据实际情况灵活多变的去写

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值