prority_queue 的用法 实例

①默认的队列 int按照从大至小

priority_queue<int>;


②将pair pa按pa.first从小至大排列

typedef pair<int,int> pa;
priority_queue<<pa,vector<pa>,greater<pa>> que;


③将int数按照从小至大排列

 priority_queue<int,vector<int>,greater<int>> que ;
///其中,第二个函数是容器类型,第三个参数是比较函数

④将node按照priority的大小从大到小排列 如果是从大到小 应该改成>吧。。

 struct node 
{
friend bool operate<()
{ return n1.priority<n2.priority(); }
int priority;
int value;
};
#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;
      priority_queue<node> qn;
    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++)
        qn.push(b[i]);
    cout<<"优先级"<<'	'<<""<<endl;
    for(i = 0; i < len; i++)
    {
        cout<<qn.top().priority<<'	'<<qn.top().value<<endl;
        qn.pop();
    }
    return 0;
}


最后代码为转,可调试最后一个用法

原文地址:https://www.cnblogs.com/weiweiyi/p/5255736.html