#include <iostream>
#include <queue>
#include <string>
using namespace std;
struct Mystuct
{
string name;
int age;
};
struct stuless
{
bool operator()(const Mystuct &s1, const Mystuct &s2) //重载()
{
return s1.age < s2.age;
}
};
int main(void)
{
Mystuct s1;
s1.name = "卡卡西";
s1.age = 12;
Mystuct s2;
s2.name = "可可西里";
s2.age = 13;
Mystuct s3;
s3.name = "五月天";
s3.age = 123;
priority_queue<Mystuct, vector<Mystuct>, stuless>my; //仿函数
my.push(s1);
my.push(s2);
my.push(s3);
//升序排列
while (!my.empty())
{
std::cout << my.top().name << " " << my.top().age << endl;
my.pop();
}
cin.get();
return 0;
}
C++的STL值优先队列
最新推荐文章于 2024-08-11 14:27:36 发布