不使用vector解决上述问题
时间: 2023-09-01 14:13:46 浏览: 29
好的,这里是不使用 vector 的 C++ 实现的代码:
```c++
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct Student {
string name;
double score;
bool operator<(const Student& other) const {
return score < other.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);
cout << maxIt->name << " " << fixed << setprecision(1) << maxIt->score << endl;
return 0;
}
```
这里使用了数组 `students` 存储学生信息,然后使用 `max_element` 函数找到成绩最高的学生。其他部分与上一个实现的代码类似。
需要注意的是,使用数组时必须指定数组的大小,这里的数组大小为 5。