- 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;
}