[IT]/알고리즘
[알고리즘] stl 정렬 알고리즘 사용하기. (C++)
working_hard
2019. 5. 31. 15:08
728x90
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Student {
public:
string name;
int score;
Student(string name, int score) {
this->name = name;
this->score = score;
}
// 정렬 기준은 '점수가 작은 순서'
bool operator <(Student &student) {
return this->score < student.score;
}
};
int main(void) {
Student students[] = {
Student("유동관", 90),
Student("김범현", 93),
Student("권오현", 97),
Student("강종구", 87),
Student("이태일", 92)
};
sort(students, students + 5);
for (int i = 0; i < 5; i++) {
cout << students[i].name << ' ';
}
}