티스토리 뷰

1. 비교 함수 사용

#include <iostream>

#include <vector>

#include <algorithm>

using namespace std;


struct Point {

int x, y;

};


bool cmp(const Point& u, const Point& v) {

if (u.x < v.x)

return true;

else if (u.x == v.x)

return u.y < v.y;

else

return false;

}


int main() {

int n;

cin >> n;

vector<Point> a(n);

for (int i = 0; i < n; i++) {

cin >> a[i].x;

cin >> a[i].y;

}

sort(a.begin(), a.end(), cmp);

for (int i = 0; i < n; i++) {

cout << a[i].x << " " << a[i].y << '\n';

}

return 0;

}


2. 연산자 오버로딩 사용

#include <iostream>

#include <algorithm>

#include <vector>

using namespace std;



struct Point {

int x, y;

bool operator <(const Point& v) const {

if (x < v.x)

return true;

else if (x == v.x) 

return y < v.y;

else

return false;

}

};


int main(void) {

int n;

cin >> n;

vector<Point> a(n);

for (int i = 0; i < n; i++) {

cin >> a[i].x;

cin >> a[i].y;

}

sort(a.begin(), a.end());

for (int i = 0; i < n; i++) {

cout << a[i].x << " " << a[i].y << '\n';

}

return 0;

}

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/06   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
글 보관함