不使用operator解决上述问题
时间: 2023-09-01 14:13:46 浏览: 40
好的,这里是不使用 `operator` 的 C++ 实现的代码:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string name;
double score;
};
bool compareStudent(const Student& a, const Student& b) {
return a.score < b.score;
}
int main() {
Student students[5];
// 从键盘输入5个学生的信息,存储到数组中
for (int i = 0; i < 5; ++i) {
cin >> students[i].name >> students[i].score;
}
// 找出成绩最高的学生
auto maxIt = max_element(students, students + 5, compareStudent);
cout << maxIt->name << " " << fixed << setprecision(1) << maxIt->score << endl;
return 0;
}
```
这里使用了函数 `compareStudent` 来比较学生的成绩大小,然后在 `max_element` 函数中使用该函数来找到成绩最高的学生。其他部分与上一个实现的代码类似。
需要注意的是,使用函数比较时,需要在 `max_element` 函数中传入比较函数的指针,即 `compareStudent`。