Есть много способов выполнить sh эту задачу, используя std :: sort.
- добавив оператор <() для структурирования ученика </li>
struct student {
string name;
int number;
double score = 0;
bool operator < (const student& other) const {
return this->score < other.score;
}
};
// Then just simply use std::sort
std::sort(std::begin(stu), std::end(stu));
Если вы не можете изменить структуру студентов, вы можете определить функцию сравнения
// define a function that compare students by score:
bool compareScore (const student& a, const student& b) {
return a.score < b.score;
}
// then use std::sort with the compare function
std::sort(std::begin(stu), std::end(stu), compareScore);
Вы также можете использовать функтор для выполнения работы
struct studentFunctor {
bool operator()(const student& a, const student& b) {
return a.score < b.score;
}
};
// then use std::sort with the functor
std::sort(std::begin(stu), std::end(stu), studentFunctor());
Использование C ++ 11 лямбда, который является предпочтительным способом сортировки
std::sort(std::begin(stu), std::end(stu),
[](const student& a, const student& b){return a.score < b.score;})