#include <iostream> #include <functional> #include <queue> using namespace std; struct node { friend bool operator<(node n1,node n2) { return n1.priority<n2.priority; } int priority; int value; }; int main() { const int len=5; int i; int a[len]={3,5,9,6,2}; //优先队列中从大到小输出 priority_queue<int>q1; for(i=0;i<len;i++) q1.push(a[i]); for(int i=0;i<len;i++) { cout<<q1.top(); q1.pop(); } cout<<endl; //优先队列中从小到大输出 priority_queue<int,vector<int>,greater<int> >q2; for(i=0;i<len;i++) q2.push(a[i]); for(i=0;i<len;i++) { cout<<q2.top(); q2.pop(); } cout<<endl; //按照某个优先级输出,该代码中为priority值大的先输出 priority_queue<node>q3; node b[len]; b[0].priority=6;b[0].value=1; b[1].priority=9;b[1].value=5; b[2].priority=2;b[2].value=3; b[3].priority=8;b[3].value=2; b[4].priority=1;b[4].value=4; for(i=0;i<len;i++) q3.push(b[i]); cout<<"优先级"<<' '<<"值"<<endl; for(i=0;i<len;i++) { cout<<q3.top().priority<<' '<<q3.top().value<<endl; q3.pop(); } return 0; }