加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

(转)C/C++sort从大到小排序

(2019-07-23 20:21:06)
分类: c/cpp
C/C++ sort从大到小排序
方法:重写sort中的cmp方法

sort方法默认的是从小到大排序,如果希望能按照从大到小的方式排序,需要重写cmp方法

原文:https://blog.csdn.net/pack__pack/article/details/80773972 

#include
#include
 
using namespace std;
 
bool cmp(int a,int b){
    return a > b;
}
 
int main(){
    int a[] = {5,3,7,3,9,4};
    sort(a,a+6,cmp);
    for(int i = 0;i < 6;i ++){
        cout<<a[i]<<" ";
    }
    return 0;
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有