简介:

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

成员函数(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. [NOIP2012]借教室 题解

    题目大意: 有一个n个数的数列,m个操作,第i个操作使[li,ri]区间建di,问第几个操作使数列中出现负数. 思路: 暴力显然过不了,那么就可以优化了,不难想到线段树,显然需要良好的姿势,那么就差分 ...

  2. 一言不合敲代码(1)——DIV+CSS3制作哆啦A梦头像

    先展示一下我的头像吧. 作为一个前端ER,我的头像当然不能是绘画工具画出来的.没错,这个玩意是由HTML+CSS代码实现的,过年的某一天晚上无聊花了一个小时敲出来的.来看看它原本的样子: 为什么会变成 ...

  3. jQueryt过滤选择器

    jQueryt过滤选择器 基本过滤选择器 选择器 描述 返回 示例 重要 :first 返回第一个元素 单个元素     :last 返回最后一个元素 单个元素     :not(selector) ...

  4. CF #374 (Div. 2) D. 贪心,优先队列或set

    1.CF #374 (Div. 2)   D. Maxim and Array 2.总结:按绝对值最小贪心下去即可 3.题意:对n个数进行+x或-x的k次操作,要使操作之后的n个数乘积最小. (1)优 ...

  5. ZeroMQ接口函数之 :zmq_getsockopt – 获取ZMQ socket的属性

    ZeroMQ API 目录 :http://www.cnblogs.com/fengbohello/p/4230135.html 本文地址 :http://www.cnblogs.com/fengbo ...

  6. cxf客户端动态调用空指针异常

    异常信息如下: 二月 , :: 上午 org.apache.cxf.common.jaxb.JAXBUtils logGeneratedClassNames 信息: Created classes: ...

  7. Invoke-Command和-ComputerName 效率比较

    看到网上有文章说Invoke-Command的方式相较其他方式的效率要高,特地试验了一下,但是这个实验不是很好: 机器只有2台 0. 用Get-WinEvent,日志数=200,Invoke方式快 1 ...

  8. infinitynewtab 背景api

    http://img.infinitynewtab.com/wallpaper/527.jpg 图片   1-4050

  9. 掌握Thinkphp3.2.0----内置标签

    使用内置标签的时候,一定要注意闭合-----单标签自闭合,双标签对应闭合 标签的学习在于记忆和应用 一. 判断比较 //IF 语句的完整格式 <if condition="$user ...

  10. 基于fab自动化部署

    fab是一个python库,强大好使,可以做很多帮助你减轻工作量的事情,比如在多台服务器上部署web项目,这里就讲讲使用它简单的方法来执行部署的过程. 关于fab的安装的基本使用,网上一搜一大把,内容 ...