【C++】二維陣列按列排序

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

bool cmp(vector<int> a, vector<int> b){
    return a[0] < b[0];
}

int main(){
    vector<vector<int>> v{{2, 2, 1},
                          {3, 1, 2},
                          {1, 3, 3}};

    sort(v.begin(), v.end(), cmp);

    int r = v.size(); // Rows
    int c = v[0].size(); // Columns

    for(int i=0; i<r; i++){
        for(int j=0; j<c; j++){
            cout << v[i][j] << ' ';
        }
        cout << '\n';
    }
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *