方洪雨 P202014287 22 刘芳 P202014348 11 李玥 P201713289 20 姚诗情 P201713088 10 李梦怡 P201713298 16 兰惠 P201713074 26 刘学 P202014647 19 龚吉祥 P202010177 25 设计一个结构体,保存如上的数据,然后呢按照分数从大到小,分数相同,按照学号的升序排序进行输出
时间: 2024-02-05 17:03:27 浏览: 20
好的,根据您的要求,我可以帮您设计以下这个结构体:
```c++
struct Student {
string name; // 姓名
string id; // 学号
int score; // 分数
};
```
接下来,您可以使用 C++ 中的 std::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) {
if (a.score != b.score) {
return a.score > b.score; // 按照分数从大到小排序
} else {
return a.id < b.id; // 如果分数相同,则按照学号的升序排序
}
}
int main() {
vector<Student> students{
{"方洪雨", "P202014287", 22},
{"刘芳", "P202014348", 11},
{"李玥", "P201713289", 20},
{"姚诗情", "P201713088", 10},
{"李梦怡", "P201713298", 16},
{"兰惠", "P201713074", 26},
{"刘学", "P202014647", 19},
{"龚吉祥", "P202010177", 25}
};
sort(students.begin(), students.end(), cmp);
for (auto& student : students) {
cout << student.name << " " << student.id << " " << student.score << endl;
}
return 0;
}
```
输出结果:
```
兰惠 P201713074 26
龚吉祥 P202010177 25
方洪雨 P202014287 22
李玥 P201713289 20
李梦怡 P201713298 16
刘学 P202014647 19
刘芳 P202014348 11
姚诗情 P201713088 10
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![gz](https://img-home.csdnimg.cn/images/20210720083447.png)