知无涯之std::sort源码剖析
https://blog.csdn.net/qq_16836151/article/details/51527160
sort函数头文件<iostream>
例题:王道机试指南
sort(T a , T b , bool function)
参数a为排序起始点,参数b为排序终点,function为排序规则
cmp()返回值为bool,cmp中定义判断规则,通俗来讲,就是将排序规则直接进行翻译,返回值为符合要求的布尔表达式
//返回要求的情况 即 排序要求
bool cmp(Student a,Student b) {
if (a.grades != b.grades) return a.grades < b.grades;
else
{
if (strcmp(a.name, b.name) != 0)
return strcmp(a.name, b.name) < 0;
else
return a.age < b.age;
}
}
//主操作函数
void WD2_2() {
int Count = 0;
//cin >> Count;
while (cin >> Count)
{
for (int i = 0; i < Count; i++)
{
cin >> student[i].name;
cin >> student[i].age;
cin >> student[i].grades;
}
sort(student, student + Count, cmp);
for (int i = 0; i < Count; i++)
{
cout << student[i].name << " " << student[i].age << " " << student[i].grades << endl;
}
return;
}
}