C & CPP
<algorithm>라이브러리 sort 함수 - 연산자 오버로딩 사용 예
에드윈H
2021. 1. 8. 23:46
연산자 오버로딩을 이용한 sort
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
struct Data {
int money;
int when;
Data(int a, int b) {
money = a;
when = b;
}
bool operator<(const Data &b)const{ //연산자 오버로딩 호출
return when > b.when;
}
};
int main() {
int n, i, j, a, b, res = 0, max = -2147000000;
vector<Data> T;
priority_queue<int> pQ;
scanf_s("%d", &n);
for (i = 1; i <= n; i++) {
scanf_s("%d %d", &a, &b);
T.push_back(Data(a, b));
if (b > max)
max = b;
}
sort(T.begin(), T.end()); //정렬시 13번줄
return 0;
}
사용 예
[백준 11650: 좌표 정렬하기] - C++
www.acmicpc.net/problem/11650 11650번: 좌표 정렬하기 첫째 줄에 점의 개수 N (1 ≤ N ≤ 100,000)이 주어진다. 둘째 줄부터 N개의 줄에는 i번점의 위치 xi와 yi가 주어진다. (-100,000 ≤ xi, yi ≤ 100,000)..
hyo-ue4study.tistory.com