728x90
반응형

단일 vector만 사용했다면 sort()를 사용해서 정렬할 수 있다.

하지만 vector 와 pair를 같이 사용했을 때에는 람다나 비교 함수를 사용해서 정렬을 해주어야한다.

지금은 람다를 이용해서 정렬하는 경우다.

#include <vector>
#include <algorithm>

vector<pair<type, type> > v;

// first 로 정렬하기
sort(v.begin(), v.end(), [](const pair<type, type> &a, const pair<type, type> &b) {
             return a.first > b.first;
    });

// second 로 정렬하기
sort(v.begin(), v.end(), [](const pair<type, type> &a, const pair<type, type> &b) {
             return a.second > b.second;
    });

 

728x90
반응형
복사했습니다!