使用lambda表达式来完成sort()
函数的第3个参数,十分优雅,强烈推荐。
语法为
[](Student a, Student b){return a.ChineseGrade >= b.ChineseGrade;}
完整程序如下,
#include
#include
using namespace std;
struct Student
{
string name;
int MathGrade;
int ChineseGrade;
};
int main()
{
Student students[5];
students[0] = {"赵", 10, 20};
students[1] = {"钱", 20, 40};
students[2] = {"孙", 50, 30};
students[3] = {"李", 40, 10};
students[4] = {"王", 30, 50};
//语文成绩大的学生排在前面,结构体中的变量ChineseGrade
sort(students, students+5, [](Student a, Student b){return a.ChineseGrade >= b.ChineseGrade;});
for(int i = 0; i < 5; i++)
cout << "name = " << students[i].name << ", mathgrade = " << students[i].MathGrade << ", chinesegrade = " << students[i].ChineseGrade << endl;
return 0;
}
程序输出为,
name = 王, mathgrade = 30, chinesegrade = 50
name = 钱, mathgrade = 20, chinesegrade = 40
name = 孙, mathgrade = 50, chinesegrade = 30
name = 赵, mathgrade = 10, chinesegrade = 20
name = 李, mathgrade = 40, chinesegrade = 10
在结构体内部重载小于号bool operator < (const Student& tmp) const
,
#include
#include
using namespace std;
struct Student
{
string name;
int MathGrade;
int ChineseGrade;
//重载小于号,语文成绩大的学生排在前面,结构体中的变量ChineseGrade
bool operator< (const Student& tmp) const
{
return ChineseGrade > tmp.ChineseGrade;
}
};
int main()
{
Student students[5];
students[0] = {"赵", 10, 20};
students[1] = {"钱", 20, 40};
students[2] = {"孙", 50, 30};
students[3] = {"李", 40, 10};
students[4] = {"王", 30, 50};
sort(students, students+5);
for(int i = 0; i < 5; i++)
cout << "name = " << students[i].name << ", mathgrade = " << students[i].MathGrade << ", chinesegrade = " << students[i].ChineseGrade << endl;
return 0;
}
输出为,
name = 王, mathgrade = 30, chinesegrade = 50
name = 钱, mathgrade = 20, chinesegrade = 40
name = 孙, mathgrade = 50, chinesegrade = 30
name = 赵, mathgrade = 10, chinesegrade = 20
name = 李, mathgrade = 40, chinesegrade = 10
双条件排序,代码示例如下:
struct projectionRoadboundary {
double minoffset = 0.0;
double maxoffset = 0.0;
int markingid = 0;
//按照minoffset从小到大排序,如果minoffset相近,则按照maxoffset从小到大排序
bool operator<(const projectionRoadboundary& tmp) const {
return minoffset < tmp.minoffset || (abs(minoffset - tmp.minoffset) <= 1e-6 && maxoffset < tmp.maxoffset);
}
};
在结构体外写bool cmp(Student a, Student b)
函数,
#include
#include
using namespace std;
struct Student
{
string name;
int MathGrade;
int ChineseGrade;
};
bool cmp(Student a, Student b)
{
return a.ChineseGrade > b.ChineseGrade;
}
int main()
{
Student students[5];
students[0] = {"赵", 10, 20};
students[1] = {"钱", 20, 40};
students[2] = {"孙", 50, 30};
students[3] = {"李", 40, 10};
students[4] = {"王", 30, 50};
//语文成绩大的学生排在前面,结构体中的变量ChineseGrade
sort(students, students+5, cmp);
for(int i = 0; i < 5; i++)
cout << "name = " << students[i].name << ", mathgrade = " << students[i].MathGrade << ", chinesegrade = " << students[i].ChineseGrade << endl;
return 0;
}
输出为,
name = 王, mathgrade = 30, chinesegrade = 50
name = 钱, mathgrade = 20, chinesegrade = 40
name = 孙, mathgrade = 50, chinesegrade = 30
name = 赵, mathgrade = 10, chinesegrade = 20
name = 李, mathgrade = 40, chinesegrade = 10