qfedu-cpp-level/day10/stl_set_demo/d4.cpp

60 lines
994 B
C++
Raw Normal View History

2023-08-14 17:20:39 +08:00
// 自定义排序
#include <iostream>
#include <set>
using namespace std;
// 自定义规则
template <typename T>
class MySort
{
public:
bool operator()(const T &a, const T &b)
{
return a > b; // 从小到大排序
}
};
class Student
{
public:
string name;
int age;
float score;
public:
Student(const string &name, int age, float score)
{
this->name = name;
this->age = age;
this->score = score;
}
};
class MyStudentByAgeSort
{
public:
bool operator()(const Student &s1, const Student &s2)
{
return s1.age > s2.age;
}
};
class MyStudentByScoreSort
{
public:
bool operator()(const Student &s1, const Student &s2)
{
return s1.score > s2.score;
}
};
int main()
{
Student[] *stus = new Student[3]{Student("张三", 18, 100), Student("李四", 20, 90), Student("王五", 19, 95)};
set<Student, MyStudentByAgeSort> s2;
s2............................
return 0;
}