简介:

  优先队列是一种容器适配器,优先队列的第一个元素总是最大或最小的(自定义的数据类型需要重载运算符)。它是以堆为基础实现的一种数据结构。

成员函数(Member functions)

(constructor): Construct priority queue (public member function)
empty: Test whether container is empty (public member function)
size: Return size (public member function)
top: Access top element (public member function)
push: Insert element (public member function)
pop: Remove top element (public member function)

代码示例

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<functional>
#include<queue>
#include<vector> using namespace std;
int arr[] = {1, 9, 2, 8, 3, 7, 4, 5, 3, 5, 10, 8, 9}; struct node
{
friend bool operator < (node n1, node n2)
{
return n1.index < n2.index;
}
friend bool operator > (node n1, node n2)
{
return n1.index > n2.index;
} int index;
int value;
}; node b[]= {{10,100},
{99,50000},
{23,33},
{44,132},
{66,44}
}; int main()
{
//1.常见用法,默认最大元素优先
priority_queue<int> pq1;
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
pq1.push(arr[i]);
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
{
cout<<pq1.top()<<' ';
pq1.pop();
}
cout<<endl; //2.注意优先队列的申明方式
priority_queue<int,vector<int>, greater<int> > pq2;
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
pq2.push(arr[i]);
for(int i = 0 ; i < sizeof(arr) / sizeof(int); i++)
{
cout<<pq2.top()<<' ';
pq2.pop();
}
cout<<endl; //3.使用自定义的数据类型
priority_queue<node> pq3;
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
pq3.push(b[i]);
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
{
cout<<pq3.top().index<<' '<<pq3.top().value<<endl;
pq3.pop();
}
cout<<endl;
//4.
priority_queue<node, vector<node>, greater<node> > pq4;
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
pq4.push(b[i]);
for(int i = 0; i < sizeof(b) / sizeof(node); i++)
{
cout<<pq4.top().index<<' '<<pq4.top().value<<endl;
pq4.pop();
} return 0;
}

  

STL-<queue>-priority queue的使用的更多相关文章

  1. [Algorithms] Queue & Priority Queue

    In this lesson, you will learn how to create a queue in JavaScript. A queue is a first-in, first-out ...

  2. [置顶] ※数据结构※→☆线性表结构(queue)☆============优先队列 链式存储结构(queue priority list)(十二)

    优先队列(priority queue) 普通的队列是一种先进先出的数据结构,元素在队列尾追加,而从队列头删除.在优先队列中,元素被赋予优先级.当访问元素时,具有最高优先级的元素最先删除.优先队列具有 ...

  3. STL之heap与优先级队列Priority Queue详解

    一.heap heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制.而这个实现机制中的m ...

  4. stl源码分析之priority queue

    前面两篇介绍了gcc4.8的vector和list的源码实现,这是stl最常用了两种序列式容器.除了容器之外,stl还提供了一种借助容器实现特殊操作的组件,谓之适配器,比如stack,queue,pr ...

  5. find-median-from-data-stream & multiset priority queue 堆

    https://leetcode.com/problems/find-median-from-data-stream/ 这道题目实在是不错,所以单独拎出来. https://discuss.leetc ...

  6. [Algorithm] Heap & Priority queue

    这里只是简单的了解,具体内容详见推荐的原链接 注意堆和树的区别 堆就是优先级队列的实现形式 堆排序 排序过程 Ref: 排序算法之堆排序(Heapsort)解析 第一步(构造初始堆): {7, 5, ...

  7. 优先队列(Priority Queue)

    优先队列(Priority Queue) A priority queue must at least support the following operations: insert_with_pr ...

  8. Objective-C priority queue

    http://stackoverflow.com/questions/17684170/objective-c-priority-queue PriorityQueue.h // // Priorit ...

  9. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅴ

    命题Q.对于一个含有N个元素的基于堆叠优先队列,插入元素操作只需要不超过(lgN + 1)次比较,删除最大元素的操作需要不超过2lgN次比较. 证明.由命题P可知,两种操作都需要在根节点和堆底之间移动 ...

随机推荐

  1. 单元测试地二蛋 先弄个两个原生模块1个原始的一个jq插件

    放羊测试测完了再测这两个瞎搞的下拉列表组建 看看从单元测试模块化的角度组建会写成啥样 1:ajax请求 简单文本     2:1个页面多个实例     3:复杂展示+自定义点击+自定义处理函数     ...

  2. Nginx日常操作和配置

    安装位置:/usr/local/nginx配置目录:/usr/local/nginx/conf配置文件:/usr/local/nginx/conf/nginx.conf启动命令:/usr/local/ ...

  3. 解决使用osgModeling的Loft生成管子时的bug

    最近在使用osgModeling的Loft生成管子的时候, 发现这个类还是有点bug的. 具体的表现就是在某些情况下, 生成管子的某些节点会是扁的, 而且有时管子会莫名的变粗.   在网上各种求助无果 ...

  4. ZeroMQ接口函数之 :zmq_ctx_term - 终结一个ZMQ环境上下文

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_ctx_term zmq_ctx_term(3) ØMQ Manual - ØMQ/4.1.0 Name zmq_ ...

  5. mongoVUE的增删改查操作使用说明

    mongoVUE的增删改查操作使用说明 一. 查询 1. 精确查询 1)右键点击集合名,再左键点击Find 或者直接点击工具栏上的Find 2)查询界面,包括四个区域 {Find}区,查询条件格式{& ...

  6. [Android]关于Installation error: INSTALL_PARSE_FAILED_MANIFEST_MALFORMED ,修改包名

    查了很多,说修改manifest.本来是没有任何的修改,自动生成的,最后发现参考了人家的一篇: http://bbs.csdn.net/topics/390613702 修改包名,包名带了大些的开头了 ...

  7. Oracle数据库字符集试验

    对于初学者我们可以理解字符集就是一种字符编码方式,试想人可以直接语言进行交流,使用文字进行记录,而计算机却不认得我们人类创立的文字,计算机只认得0和1这样的二进制代码.当我们要通过计算机记录文字信息的 ...

  8. iOS CADisplayLink 定时器的使用

    CADisplayLink 是一个能让我们以和屏幕刷新频率相同的频率将内容刻画到屏幕上的定时器,在应用中创建一个新的CADisplayLink对象,把他添加到一个runloop中,并且给他提供一个ta ...

  9. 【6集iCore3_ADP触摸屏驱动讲解视频】6-1 工程及程序构架介绍

    视频简介: 该视频由银杏科技有限公司基于iCore3应用开发平台推出,包含 触摸屏驱动工程文件的介绍与程序构架的介绍等.   源视频包下载地址: http://pan.baidu.com/s/1dFz ...

  10. Get请求中文乱码的几种解决方式

    1.将字符串转码:new String("xxxxx".getBytes("iso-8859-1"),"utf-8")         这种 ...