공부/C, C++

[C++] sort( )를 이용한 구조체 정렬

happyst 2022. 1. 12. 01:05

 

#include <stdio.h>
#include <iostream>
#include <algorithm>

using namespace std;

int N;
struct st {
	int start, end;
};

struct st meeting[100000 + 10];

bool cmp(const struct st &m1, const struct st &m2) {
	if (m1.start < m2.start)
	{
		return true;
	}
	else if (m1.start == m2.start)
	{
		return m1.end < m2.end;
	}
	else
	{
		return false;
	}
}

int main(void)
{
	scanf("%d", &N);
	for (int i = 0; i < N; i++)
	{
		scanf("%d %d", &meeting[i].start, &meeting[i].end);
	}

	sort(meeting, meeting + N, cmp);

	for (int i = 0; i < N; i++)
	{
		printf("%d %d\n", meeting[i].start, meeting[i].end);
	}
	return 0;
}