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
반응형
'프로그래밍 언어 > C++' 카테고리의 다른 글
[C++] 표준 입력 함수 정리 (0) | 2019.08.15 |
---|---|
C++ AND , OR , XOR 비트 연산자 (0) | 2019.08.15 |
C++ 문자를 2진수로 출력하기 (0) | 2019.08.15 |
[C++] 소수점 자릿수 출력하기 (0) | 2019.07.31 |
C++ vector 에서 erase 함수 사용할 때 주의할 점 (0) | 2019.07.26 |