프로그래밍 언어/C++
vector<pair<type, type> > 람다 이용해서 정렬하기
남제이입니다!
2019. 9. 1. 13:12
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
반응형