#include<iostream>
#include<queue>
#include<string>
using namespace std;
//优先队列相关
class Student
{
public:
Student(string name,int age):name(name),age(age){}
string GetName() const
{
return this->name;
}
int GetAge() const
{
return this->age;
}
bool operator< (const Student& S) const
{
return this->age < S.age;
}
private:
int age;
string name;
};
int main(void)
{
priority_queue<Student, vector<Student>, less<Student>> num;
num.push(Student("张三", 18));
num.push(Student("李四", 19));
num.push(Student("王五", 20));
while (!num.empty())
{
cout << num.top().GetName() << " " << num.top().GetAge() << endl;
num.pop();
}
return 0;
}
可能会遇到的问题:
由于top函数是const函数,所以类中的getname相关的函数也应为const
bool重载<号是重载函数也必须要是const修饰的函数