STL的优先队列如果对于结构体入队比较复杂,特此笔记
#include<bits/stdc++.h>
#define UP(i,x,y) for(int i=x; i<=y; i++)
using namespace std;
struct student{
int age;
string name;
/*
默认大根堆
student类型的age作为比较
*/
bool operator < (const student & pam) const
{
return age < pam.age; /* 若需要小根堆将此处改为> */
}
};
/* 默认大根堆 */
priority_queue<student> pq;
int main()
{
student a1,a2,a3;
a1.age = 100;
a2.age = 101;
a3.age = 200;
a1.name = "ll";
a2.name = "aa";
a3.name = "cc";
pq.push(a1);
pq.push(a2);
pq.push(a3);
while(pq.empty() == false)
{
cout<<pq.top().age<<endl;
pq.pop();
}
return 0;
}