https://www.acmicpc.net/problem/11650
두 가지 기준으로 정렬하는 문제이다.
algorithm
라이브러리의 sort
함수의 비교함수를 새로 만들어서 사용하면 된다.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool comp(pair<int, int> a, pair<int, int> b) {
if(a.first == b.first) return a.second < b.second;
return a.first < b.first;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n;
cin >> n;
vector<pair<int, int>> v;
while(n--) {
int x, y;
cin >> x >> y;
v.push_back({x, y});
}
sort(v.begin(), v.end(), comp);
for(auto& e : v) cout << e.first << " " << e.second << "\n";
return 0;
}