c++的一些排序(数组,map)

一、数组

1、普通数组升序

数组升序

void displayArr(int* arr) {
    for(int i = 0; i < 10 ; i++)
        printf("  %d\t",arr[i]);
    printf("\n");
}

void sortArr() {
    int arr[10] = {5,43,654,88,1,6,40,7777,13,5};
    displayArr(arr);
    cout << endl;

    //普通排序
    sort(arr,arr + 10);
    printf("the normal sort is  : ");
    displayArr(arr);
}

console

the normal sort is  :   1         5       5       6       13      40      43      88      654     7777

2、自定义升序

数组自定义

void displayArr(int* arr) {
    for(int i = 0; i < 10 ; i++)
        printf("  %d\t",arr[i]);
    printf("\n");
}

void sortArr() {
    int arr[10] = {5,43,654,88,1,6,40,7777,13,5};
    displayArr(arr);
    cout << endl;
    
    //自定义 从小到大排列
    sort(arr,arr + 10 , less<int>());
    printf("the less sort is  : ");
    displayArr(arr);
}

console

the less sort is  :   1   5       5       6       13      40      43      88      654     7777

3、自定义降序

void displayArr(int* arr) {
    for(int i = 0; i < 10 ; i++)
        printf("  %d\t",arr[i]);
    printf("\n");
}

void sortArr() {
    int arr[10] = {5,43,654,88,1,6,40,7777,13,5};
    displayArr(arr);
    cout << endl;

    //自定义 从大到小排列
    sort(arr,arr + 10 , greater<int>());
    printf("the greater sort is  : ");
    displayArr(arr);
}

console

the greater sort is  :   7777     654     88      43      40      13      6       5       5       1

二、map排序

1、map默认顺序

map 默认按照key的大小从小到大排序

void displayMap(map<int,float> mm) {
    map<int,float>::iterator iter = mm.begin();
    for(;iter != mm.end();iter ++)
        printf("[%d , %f]\t",iter->first,iter->second);
    cout<<endl;
}

void sortMap() {
    map<int,float> nums;
    nums[0] = 0.6;
    nums[1] = 0.8;
    nums[2] = 0.16;
    nums[5] = 1.6;
    nums[3] = 0.663;
    nums[4] = 0.9;
    printf("normal sort:\t");
    displayMap(nums);
}

console

normal sort:
        [0 , 0.600000]
        [1 , 0.800000]
        [2 , 0.160000]
        [3 , 0.663000]
        [4 , 0.900000]
        [5 , 1.600000]

2、按照key降序

创建map时候添加自定义排序方式

void displayMap(map<int,float,greater<int>> mm) {
    map<int,float,greater<int>>::iterator iter = mm.begin();
    for(;iter != mm.end();iter ++)
        printf("\t[%d , %f]\n",iter->first,iter->second);
    cout<<endl;
}

void sortMap() {    
    map<int,float,greater<int>> numsGreater;
    numsGreater[0] = 0.6;
    numsGreater[1] = 0.8;
    numsGreater[2] = 0.16;
    numsGreater[5] = 1.6;
    numsGreater[3] = 0.663;
    numsGreater[4] = 0.9;
    printf("greater sort:\n");
    displayMap(numsGreater);
}

console

greater sort:
        [5 , 1.600000]
        [4 , 0.900000]
        [3 , 0.663000]
        [2 , 0.160000]
        [1 , 0.800000]
        [0 , 0.600000]

3、按照value排序

需要将map,转换为vector<paire<xxx,xxx>>,再使用sort排序

bool cmpVect(pair<int,float> a,pair<int,float> b) {
    return a.second > b.second;
}

void displayVect(vector<pair<int,float>> mm) {
    vector<pair<int,float>>::iterator iter = mm.begin();
    for(;iter != mm.end();iter ++)
        printf("\t[%d , %f]\n",iter->first,iter->second);
    cout<<endl;
}

void sortMap() {
    vector<pair<int,float>> sortVect(nums.begin(),nums.end());
    sort(sortVect.begin(),sortVect.end(),cmpVect);
    printf("greater float sort:\n");
    displayVect(sortVect);
}

console

greater float sort:
        [5 , 1.600000]
        [4 , 0.900000]
        [1 , 0.800000]
        [3 , 0.663000]
        [0 , 0.600000]
        [2 , 0.160000]

T.B.C.

参考博文:
1.https://blog.csdn.net/VariatioZbw/article/details/125155432
2.https://blog.csdn.net/qq_41076797/article/details/109818070

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值