c++结构体成绩排序
时间: 2023-12-19 16:06:30 浏览: 265
以下是使用结构体数组实现c++成绩排序的示例代码:
```cpp
#include <iostream>
#include <algorithm>
using namespace std;
struct student{
int score;
string name;
};
bool cmp(student a, student b){
return a.score > b.score; // 按照成绩从高到低排序
}
int main(){
const int N = 5; // 学生人数
student stu[N] = {{90, "Tom"}, {80, "Jerry"}, {95, "Alice"}, {70, "Bob"}, {85, "David"}};
sort(stu, stu + N, cmp); // 使用sort函数进行排序
for(int i = 0; i < N; i++){
cout << stu[i].name << " " << stu[i].score << endl; // 输出排序后的结果
}
return 0;
}
```
相关问题
c++结构体sort排序
### 如何在 C++ 中使用 `std::sort` 对结构体数组进行排序
在 C++ 中,可以利用 `std::sort` 函数对结构体数组进行排序。为了实现这一功能,通常需要提供一个自定义的比较函数或 lambda 表达式来指定排序依据。
以下是具体方法及其示例代码:
#### 方法概述
1. 定义一个结构体类型。
2. 创建该类型的数组或向量。
3. 提供一个二元谓词(即返回布尔值的函数),用于描述两个对象之间的大小关系。
4. 调用 `std::sort` 并传入起始迭代器、结束迭代器以及上述比较函数。
#### 示例代码
假设有一个表示学生的结构体,其中包含姓名和成绩字段。我们希望按照学生成绩降序排列他们。
```cpp
#include <iostream>
#include <vector>
#include <algorithm> // 导入 std::sort
struct Student {
std::string name;
int score;
// 可选:重载小于运算符以便简化某些情况下的调用
bool operator<(const Student& other) const {
return this->score > other.score; // 按照分数降序排列
}
};
// 自定义比较函数
bool compareByScore(const Student& a, const Student& b) {
return a.score > b.score; // 返回 true 如果 a 的分数大于 b 的分数
}
int main() {
std::vector<Student> students = {{"Alice", 85}, {"Bob", 72}, {"Charlie", 90}};
// 使用自定义比较函数进行排序
std::sort(students.begin(), students.end(), compareByScore);
// 或者使用 lambda 表达式代替显式的比较函数
std::sort(students.begin(), students.end(), [](const Student& a, const Student& b) -> bool {
return a.score > b.score; // 同样按分数降序排列
});
// 打印排序结果
std::cout << "Students sorted by descending scores:" << std::endl;
for (const auto& student : students) {
std::cout << student.name << ": " << student.score << std::endl;
}
return 0;
}
```
此程序展示了两种方式来完成对学生数据的排序操作——通过单独声明的一个外部比较函数或者借助于匿名内部类形式表达出来的 Lambda 表达式[^1]。
#### 关键点说明
- **自定义比较逻辑**:无论是作为独立函数还是嵌套在 sort 调用内的 Lambda 表达式,都需要满足严格弱序的要求。
- **STL 支持**:得益于模板机制的支持,几乎所有的 STL 容器都可以配合这些高级特性一起工作[^4]。
---
###
C++结构体快速排序
### C++ 结构体实现快速排序
为了展示如何使用C++结构体来实现快速排序,下面提供了一个完整的例子。此示例定义了一个`Student`结构体,并实现了基于成绩字段对学生记录进行降序排序的功能。
#### 定义 `Student` 结构体以及比较函数
首先创建一个名为 `Student` 的结构体,其中包含学生的姓名和分数属性。接着编写一个用于比较两个学生对象的方法,该方法作为自定义排序准则传递给标准模板库中的 `std::sort()` 函数[^4]。
```cpp
#include <algorithm>
#include <iostream>
#include <string>
struct Student {
std::string name;
int score;
// 构造函数初始化成员变量
Student(std::string n, int s) : name(n), score(s) {}
};
// 自定义比较器:按score降序排列
bool compareByScore(const Student& lhs, const Student& rhs) {
return lhs.score > rhs.score;
}
```
#### 使用 `std::sort` 对数组进行排序
接下来,在主程序中声明若干个 `Student` 类型的对象组成的数组,并调用 `std::sort()` 来对其进行排序。注意这里传入了指向数组首地址的指针以及长度参数,还有之前定义好的比较器[^3]。
```cpp
int main() {
// 初始化一组学生成绩数据
Student students[] = {
{"Alice", 87},
{"Bob", 92},
{"Charlie", 76},
{"David", 85},
{"Eve", 90}
};
size_t numStudents = sizeof(students)/sizeof(students[0]);
// 调用STL内置的sort算法完成排序工作
std::sort(&students[0], &students[numStudents], compareByScore);
// 输出排序后的结果
for (size_t i = 0; i != numStudents; ++i){
std::cout << "Name: " << students[i].name
<< ", Score: " << students[i].score << '\n';
}
return 0;
}
```
这段代码展示了如何利用C++结构体配合 STL 提供的强大工具集轻松达成复杂的数据处理需求。通过这种方式不仅可以简化编程逻辑而且提高了可读性和维护性。
阅读全文
相关推荐













