[C/C++] Sort vector by column

Onam Kwon·2022년 12월 5일
0

C/C++

목록 보기
9/12

Sort vector by column

  • 2D 벡터를 정렬할때 첫번째 원소를 기준으로 정렬하는게 아닌 n번째 원소(열)를 기준으로 정렬이 필요할 때가 있다.
  • 아래의 코드와 같이 할 수 있다.
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

void print2DVector(vector<vector<int>> &v) {
    for(const auto &row: v) {
        for(const auto &r: row) {
            cout<<r<<" ";
        } cout<<endl;
    } cout<<endl;
}

int main() {

    vector<vector<int>> v = {{4,3,5,3}, 
                     {2,6,3,7}, 
                     {6,8,5,1}, 
                     {5,6,1,5}};
    
    cout<<"Before sort by column: "<<endl;
    print2DVector(v);

    sort(v.begin(), v.end(), [](vector<int> &v1, vector<int> &v2) {return v1[3]<v2[3];});
    cout<<"After sort by last column: "<<endl;
    print2DVector(v);

    sort(v.begin(), v.end(), [](vector<int> &v1, vector<int> &v2) {return v1[2]<v2[2];});
    cout<<"After sort by second column from the back: "<<endl;
    print2DVector(v);

    return 0;
}

Github

References

profile
권오남 / Onam Kwon

0개의 댓글