3. 关于sort 指定排序规则

在sort制定规则时,请严格使用<  >不要使用<=

1. 二维数组 排序

在cmp 函数中,指定首先按照res二维数组中,第1列降序排序、第2列降序排序、第0列升序排序。

#include<iostream>
#include<vector>
#include<algorithm>
#include <string>
#include<climits>
using namespace std;

bool cmp(vector<int>a, vector<int>b) {
    if (a[1] != b[1])return a[1] > b[1];
    if (a[2] != b[2])return a[2] > b[2];
    return a[0] < b[0];
}

int main() {
    vector<vector<int>>res;
    vector<int>temp;
    temp.push_back(1);
    temp.push_back(100);
    temp.push_back(50);
    res.push_back(temp);
    temp.clear();
    temp.push_back(2);
    temp.push_back(90);
    temp.push_back(60);
    res.push_back(temp);
    temp.clear();
    temp.push_back(4);
    temp.push_back(100);
    temp.push_back(60);
    res.push_back(temp);
    temp.clear();
    temp.push_back(3);
    temp.push_back(100);
    temp.push_back(60);
    res.push_back(temp);

    sort(res.begin(), res.end(),cmp);
    for (auto it : res) {
        cout << it[0] << " " << it[1] << " " << it[2] << endl;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42813620/article/details/130496592
今日推荐