参考资料:传送门
先回顾队列的定义:
队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部。priority_queue特别之处在于,允许用户为队列中存储的元素设置优先级。这种队列不是直接将新元素放置在队列尾部,而是放在比它优先级低的元素前面。标准库默认使用<操作符来确定对象之间的优先级关系,所以如果要使用自定义对象,需要重载 < 操作符。优先队列有两种,一种是最大优先队列;一种是最小优先队列;每次取自队列的第一个元素分别是优先级最大和优先级最小的元素。
1) 优先队列的定义
包含头文件:"queue.h", "functional.h"
可以使用具有默认优先级的已有数据结构;也可以再定义优先队列的时候传入自定义的优先级比较对象;或者使用自定义对象(数据结构),但是必须重载好< 操作符。
2) 优先队列的常用操作

  • q.empty() 如果队列为空,则返回true,否则返回false
  • q.size() 返回队列中元素的个数
  • q.pop() 删除队首元素,但不返回其值
  • q.top() 返回具有最高优先级的元素值,但不删除该元素
  • q.push(item) 在基于优先级的适当位置插入新元素

其中q.top()为查找操作,在最小优先队列中搜索优先权最小的元素,在最大优先队列中搜索优先权最大的元素。q.pop()为删除该元素。优先队列插入和删除元素的复杂度都是O(lgn),所以很快。
另外,在优先队列中,元素可以具有相同的优先权。

#include<iostream>
#include<functional>
#include<queue>
#include<vector>
#include<cstdio>
using namespace std;

//定义比较结构
struct cmp1
{
    bool operator ()(int &a, int &b)
    {
        return a > b; //最小值优先
    }
};

struct cmp2
{
    bool operator ()(int &a, int &b)
    {
        return a < b; //最大值优先
    }
};

//自定义数据结构
struct number1
{
    int x;
    bool operator < (const number1 &a) const
    {
        return x > a.x; //最小值优先
    }
};
struct number2
{
    int x;
    bool operator < (const number2 &a) const
    {
        return x < a.x; //最大值优先
    }
};
int a[] = {14, 10, 56, 7, 83, 22, 36, 91, 3, 47, 72, 0};
number1 num1[] = {14, 10, 56, 7, 83, 22, 36, 91, 3, 47, 72, 0};
number2 num2[] = {14, 10, 56, 7, 83, 22, 36, 91, 3, 47, 72, 0};

int main()
{
    priority_queue<int>que;//采用默认优先级构造队列

    priority_queue<int, vector<int>, cmp1>que1; //最小值优先
    priority_queue<int, vector<int>, cmp2>que2; //最大值优先

    priority_queue<int, vector<int>, greater<int> >que3; //注意“>>”会被认为错误,
    priority_queue<int, vector<int>, less<int> >que4; ////最大值优先

    priority_queue<number1>que5; //最小优先级队列
    priority_queue<number2>que6;  //最大优先级队列

    int i;
    for(i = 0; a[i]; i++)
    {
        que.push(a[i]);
        que1.push(a[i]);
        que2.push(a[i]);
        que3.push(a[i]);
        que4.push(a[i]);
    }
    for(i = 0; num1[i].x; i++)
        que5.push(num1[i]);
    for(i = 0; num2[i].x; i++)
        que6.push(num2[i]);

    printf("采用默认优先关系:\n(priority_queue<int>que;)\n");
    printf("Queue 0:\n");
    while(!que.empty())
    {
        printf("%3d", que.top());
        que.pop();
    }
    puts("");
    puts("");

    printf("采用结构体自定义优先级方式一:\n(priority_queue<int,vector<int>,cmp>que;)\n");
    printf("Queue 1:\n");
    while(!que1.empty())
    {
        printf("%3d", que1.top());
        que1.pop();
    }
    puts("");
    printf("Queue 2:\n");
    while(!que2.empty())
    {
        printf("%3d", que2.top());
        que2.pop();
    }
    puts("");
    puts("");
    printf("采用头文件/functional/内定义优先级:\n(priority_queue<int,vector<int>,greater<int>/less<int> >que;)\n");
    printf("Queue 3:\n");
    while(!que3.empty())
    {
        printf("%3d", que3.top());
        que3.pop();
    }
    puts("");
    printf("Queue 4:\n");
    while(!que4.empty())
    {
        printf("%3d", que4.top());
        que4.pop();
    }
    puts("");
    puts("");
    printf("采用结构体自定义优先级方式二:\n(priority_queue<number>que)\n");
    printf("Queue 5:\n");
    while(!que5.empty())
    {
        printf("%3d", que5.top());
        que5.pop();
    }
    puts("");
    printf("Queue 6:\n");
    while(!que6.empty())
    {
        printf("%3d", que6.top());
        que6.pop();
    }
    puts("");
    return 0;
}
/*
运行结果 :
采用默认优先关系:
(priority_queue<int>que;)
Queue 0:
83 72 56 47 36 22 14 10  7  3

采用结构体自定义优先级方式一:
(priority_queue<int,vector<int>,cmp>que;)
Queue 1:
 7 10 14 22 36 47 56 72 83 91
Queue 2:
83 72 56 47 36 22 14 10  7  3

采用头文件"functional"内定义优先级:
(priority_queue<int,vector<int>,greater<int>/less<int> >que;)
Queue 3:
 7 10 14 22 36 47 56 72 83 91
Queue 4:
83 72 56 47 36 22 14 10  7  3

采用结构体自定义优先级方式二:
(priority_queue<number>que)
Queue 5:
 7 10 14 22 36 47 56 72 83 91
Queue 6:
83 72 56 47 36 22 14 10  7  3*/

  

【转载】STL之priority_queue的更多相关文章

  1. STL之priority_queue(优先队列)

    priority_queue是一个容器适配器,在这个容器里第一个数据元素是最大的.它的使用场景是什么样:如果12306抢票,为什么黄牛能抢这么多票,感觉12306那边的请求队列是一个优先队列,黄牛的请 ...

  2. STL 中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue>    (2)在头文件中priority_queue定义如下: nam ...

  3. STL中priority_queue小结

    (1)为了运用priority_queue,你必须包含头文件<queue>:#include<queue> (2)在头文件中priority_queue定义如下: namesp ...

  4. C++ STL之priority_queue

    STL中的priority_queue(优先队列)是一种会按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序的容器,不同优先级的情况下,top()上永远是最高优先级的数据,其底层采用的 ...

  5. STL之priority_queue使用简介

    优先队列容器也是一种从一端入队,另一端出对的队列.不同于一般队列的是,队列中最大的元素总是位于队首位置,因此,元素的出对并非按照先进先出的要求,将最先入队的元素出对,而是将当前队列中的最大元素出对. ...

  6. STL之priority_queue

    下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...

  7. 【STL】-priority_queue的用法

    初始化: priority_queue<int>                           maxPQ; priority_queue<int,vector<int& ...

  8. STL之priority_queue为复合结构排序

    priority_queue为复合结构排序: #include <iostream> #include <queue> using namespace std; struct ...

  9. C++ STL 优先队列 priority_queue 详解(转)

    转自https://blog.csdn.net/c20182030/article/details/70757660,感谢大佬. 优先队列 引入 优先队列是一种特殊的队列,在学习堆排序的时候就有所了解 ...

随机推荐

  1. 地图中插入表格——ArcMap篇

    在制作专题图的过程中,不但要有地理要素表示空间位置,经常还要在图的周围制作一些表格数据.这里对ArcMap中的插入方法进行总结. 方法一:插入对象 利用菜单中的"插入"-" ...

  2. Oracle Sales Cloud:报告和分析(BIEE)小细节1——创建双提示并建立关联(例如,部门和子部门提示)

    Oracle Sales Cloud(Oracle 销售云)是一套基于Oracle云端的客户商机管理系统,通过提供丰富的功能来帮助提高销售效率,更好地去了解客户,发现和追踪商机,为最终的销售成交 (d ...

  3. Android开发aidl使用中linkToDeath和unlinkToDeath的使用

    1.Binder死亡代理     这一节首先将介绍Binder类中比较重要的两个方法linkToDeath和unlinkToDeath.我们知道Binder是运行在服务进程,若服务端进程因为某种原因“ ...

  4. 看看C# 6.0中那些语法糖都干了些什么(终结篇)

    终于写到终结篇了,整个人像在梦游一样,说完这一篇我得继续写我的js系列啦. 一:带索引的对象初始化器 还是按照江湖老规矩,先扒开看看到底是个什么玩意. 1 static void Main(strin ...

  5. 高级数据过滤(like)

    单字符过滤 '_' select * from T_Employee where FName like  '_erry' 多字符过滤 '%' select * from T_Employee wher ...

  6. SQL优化 查询语句中,用 inner join 作为过滤条件和用where作为过滤条件的区别

    前段时间遇到一个存储过程,参数之一是一个字符串,在存储过程中,把字符串拆分成一个临时表之后存为一个key值的临时表,作为其中一个查询条件, 逻辑实现上有两种处理方式 insert into #t se ...

  7. 异常处理之“The remote certificate is invalid according to the validation praocedure.”

    参考文章:http://brainof-dave.blogspot.com.au/2008/08/remote-certificate-is-invalid-according.html 参考文章:h ...

  8. 安装linxu6.4

    RHEL6.3系统安装 进入安装界面 这里选择跳过 点击下一步 选择安装语言 选择键盘 选择系统储存方式 选择是否格式化储存设备 给安装的系统一个计算机名 选择时区 给root一个密码 可以忽略或给一 ...

  9. 关于Jmeter分布式测试在公司内的使用

    首先非常感谢虫师的文章受益匪浅 http://www.cnblogs.com/fnng/category/345478.html 今天,花了半天时间进行分布式的测试,真是纠结啊!! RT 1.在公司用 ...

  10. 【转】XenServer架构之XAPI

    一.XAPI对资源池的管理 作为XenServer的管理工具集,XAPI管理XenServer的主机,网络和存储.不管是OpenStack还是CloudStack,如果使用XenServer作为虚拟化 ...