본문 바로가기

PROGRAMMING LANGUAGE/C++

클래스 사용해서 이름과 점수 입력받고 정렬하기

#include <iostream>
#include <algorithm>
using namespace std;

class Play
{
public:
	string name;
	int score = 0;  
	Play() {}
	Play(string name, int score)
	{
		this->name = name;
		this->score = score;  
	}

	bool operator < (Play& obj)
	{
		return this->score < obj.score;
	}
};      

int main()  
{          
	int N; cin >> N;
	Play* plays = new Play[N];
	 
	for (size_t i = 0; i < N; i++)
	{
		cin >> plays[i].name;
		cin >> plays[i].score;
	}

	sort(plays, plays + N);
	

	for (int i = 0; i < N; i++)  
	{
		cout << "rank" << i + 1 << ": ";
		cout << plays[i].name << " " << plays[i].score << endl;
	}
	delete[] plays;  
	return 0;
	
}