重写仿函数(函数指针法)
#include<iostream>
#include<algorithm>
using namespace std;
struct stu{
int num;
float score;
};
bool cmp(const stu &a, const stu &b){
return a.score < b.score;
}
int main(){
stu nums[3] = {{1,98.5}, {2,88.5}, {3,68.5}};
sort(nums, nums + 3, cmp);
for(int i = 0; i < 3; ++ i)
{
cout << nums[i].score <<endl;
}
return 0;
}
重载运算符(struct内部 + struct外部)
#include<iostream>
#include<algorithm>
using namespace std;
struct stu{
int num;
float score;
bool operator<(const stu &a) const
{
return score < a.score;
}
};
int main(){
stu nums[3] = {{1,98.5}, {2,88.5}, {3,68.5}};
sort(nums, nums + 3);
for(int i = 0; i < 3; ++ i)
{
cout << nums[i].score <<endl;
}
return 0;
}
#include<iostream>
#include<algorithm>
using namespace std;
struct stu{
int num;
float score;
};
bool operator<(const stu &a, const stu &b)
{
return a.score < b.score;
}
int main(){
stu nums[3] = {{1,98.5}, {2,88.5}, {3,68.5}};
sort(nums, nums + 3);
for(int i = 0; i < 3; ++ i)
{
cout << nums[i].score <<endl;
}
return 0;
}