#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
struct Rule1 {
bool operator()(const int& a1, const int& a2)const {
return a1 % 10 < a2 % 10;
}
};
struct Student {
char name[10];
double GPA;
int age;
};
struct StudentRule1 {//排序规则
bool operator()(const Student& a1, const Student& a2)const {
return a1.GPA < a2.GPA;
}
};
int main()
{
Student Students[3] = { {"Jack",3.5,20},{"al",3.0,9},{"zty",2.0,15}
};
sort(Students, Students + sizeof(Students) / sizeof(Students), StudentRule1());
for (int i = 0; i < 3; i++)
{
printf("%f", Students[i].GPA);
}
}
太强大了,可以很方便地对数组里的元素进行排序
本文介绍如何使用C++通过自定义排序规则,对包含姓名、GPA和年龄的Student对象数组进行按GPA降序排序。通过struct和operator()实现的排序函数,展示了如何方便地管理结构体数组并保持数据有序。

被折叠的 条评论
为什么被折叠?



