- toc {:toc}
νμ΄
sort ν¨μμ compare λ₯Ό μ΄λ»κ² μ‘°μ νλκ°μ λν λ¬Έμ μ΄λ€. λ κ°μ μμΌ κ²½μ° pair, 3 κ°μ μμΌ κ²½μ°μλ tuple μ μ¬μ©ν΄μ νμ΄νλ€.
λ λ¬Έμ μμ λ€λ₯Έ 쑰건μ 첫 λ²μ§Έ μ«μλ₯Ό λ¨Όμ λΉκ΅νλκ°, λ λ²μ§Έ μ«μλ₯Ό λ¨Όμ λΉκ΅νλκ°μ λν κ²μ΄λ€. λλ¬Έμ compare ν¨μμμ μ°¨μ΄λ₯Ό λμ΄ νμ΄νλ€. < μΈ κ²½μ° μ€λ¦μ°¨μμΌλ‘ μ λ ¬, > μΈ κ²½μ° λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬νλ€.
#include <bits/stdc++.h>
using namespace std;
vector <pair<int, int>> vec;
bool compare(const pair<int, int> &a, const pair<int, int> &b){
if (a.first == b.first){
return a.second < b.second;
}
else{
return a.first < b.first;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, x, y;
cin >> n;
for(int i=0; i<n; i++){
cin >> x >> y;
vec.push_back({x, y});
}
sort(vec.begin(), vec.end(), compare);
for(int i=0; i<n; i++){
cout << vec[i].first << " " << vec[i].second << '\n';
}
return 0;
}#include <bits/stdc++.h>
using namespace std;
vector <pair<int, int>> vec;
bool compare(const pair<int, int> &a, const pair<int, int> &b){
if (a.second == b.second){
return a.first < b.first;
}
else{
return a.second < b.second;
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int n, x, y;
cin >> n;
for(int i=0; i<n; i++){
cin >> x >> y;
vec.push_back({x, y});
}
sort(vec.begin(), vec.end(), compare);
for(int i=0; i<n; i++){
cout << vec[i].first << " " << vec[i].second << '\n';
}
return 0;
}