一,万能比较,控制排序
struct Rule1{
bool operator()(const int &a,const int &b){
return (a%10) < (b%10);
}
};
这个函数真的厉害,现在来看,可以用在容器中,可以用在普通的<algorithm>中。、
其中的const int &a中,const 和 &可以省略
有一点需要注意的是,其函数计算在算法库中是需要加上括号的,而容器中不用加括号。
在多举几个例子
struct Student{
char name[20];
int id;
int score;
};
struct Rule2{
bool operator()(const Student &s1,const Student &s2){
if(s1.score != s2.score) return s1.score > s2.score;
else return (strcmp(s1.name,s2.name)<0);
}
};