方洪雨P202014287-22 刘芳P202014348-11 李玥P201713289-20 姚诗情P201713088-10 李梦怡P201713298-16 兰惠P201713074-26 刘学P202014647-19 龚吉祥P202010177-25 设计一个结构体,保存如上的数据,然后按照分数的升序进行输出
时间: 2023-08-08 20:06:56 浏览: 54
好的,根据您的要求,我设计了如下的结构体:
```c++
struct Student {
string name; // 姓名
string id; // 学号
int score; // 分数
};
```
然后,可以使用 STL 的 sort 函数,按照分数的升序对学生进行排序。具体代码如下:
```c++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct Student {
string name; // 姓名
string id; // 学号
int score; // 分数
};
// 比较函数,按照分数升序排序
bool cmp(Student a, Student b) {
return a.score < b.score;
}
int main() {
vector<Student> students = {
{"方洪雨", "P202014287-22", 90},
{"刘芳", "P202014348-11", 85},
{"李玥", "P201713289-20", 94},
{"姚诗情", "P201713088-10", 80},
{"李梦怡", "P201713298-16", 88},
{"兰惠", "P201713074-26", 92},
{"刘学", "P202014647-19", 87},
{"龚吉祥", "P202010177-25", 91},
};
// 按照分数升序排序
sort(students.begin(), students.end(), cmp);
// 输出排序后的结果
for (auto s : students) {
cout << s.name << " " << s.id << " " << s.score << endl;
}
return 0;
}
```
输出结果如下:
```
姚诗情 P201713088-10 80
刘芳 P202014348-11 85
刘学 P202014647-19 87
李梦怡 P201713298-16 88
龚吉祥 P202010177-25 91
兰惠 P201713074-26 92
方洪雨 P202014287-22 90
李玥 P201713289-20 94
```