码迷,mamicode.com
首页 > 其他好文 > 详细

prority_queue 的用法 实例

时间:2016-03-08 23:32:21      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:

①默认的队列 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<<"优先级"<<\t<<""<<endl;
    for(i = 0; i < len; i++)
    {
        cout<<qn.top().priority<<\t<<qn.top().value<<endl;
        qn.pop();
    }
    return 0;
}


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

prority_queue 的用法 实例

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!