1、默认为大顶堆

 #include <iostream>
#include <queue>
using namespace std; int main()
{
priority_queue<int> p;
int i;
for (i = ; i < ; i++)
p.push(i);
for (i = ; i < ; i++)
{
cout << p.top() << " ";
p.pop();
}
return ;
}

运行结果:

2、小顶堆

 int main()
{
priority_queue<int, vector<int>, greater<int>> p;
//vs2013需要#include <functional>
int i;
for (i = ; i < ; i++)
p.push(i);
for (i = ; i < ; i++)
{
cout << p.top() << " ";
p.pop();
}
return ;
}

运行结果:

3、自定义

 #include <iostream>
#include <queue>
using namespace std; struct Node
{
int a, b;
Node(int x, int y) : a(x), b(y) {}
}; struct cmp
{
bool operator() (Node *x, Node *y)
{
if (x->a != y->a) return x->a < y->a; //大顶堆
return x->b < y->b;
}
};
int main()
{ priority_queue<Node *, vector<Node *>, cmp> p;
Node *n = new Node(, ); p.push(n);
n = new Node(, ); p.push(n);
n = new Node(, ); p.push(n);
while (!p.empty())
{
n = p.top();
p.pop();
cout << n->a << " " << n->b << endl;
}
return ;
}

运行结果:

c++ priority_queue的更多相关文章

  1. C++ std::priority_queue

    std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...

  2. 【转载】STL之priority_queue

    参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...

  3. STL之priority_queue

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

  4. 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动

    一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...

  5. STL之容器适配器priority_queue

    priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...

  6. priority_queue 示例

    http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...

  7. 优先队列priority_queue的比较函数

    STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...

  8. 5.1 stack,queue以及priority_queue

    *:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...

  9. hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏

    huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...

  10. priority_queue 优先队列用法

    //采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...

随机推荐

  1. POJ-1128-Frame Stacking

    链接:https://vjudge.net/problem/POJ-1128 题意: 每张图片上面画了一些边框,给出这些边框叠在一起后的图片,图片边框一定是由一个字母表示并且每条边至少三个字符,输入保 ...

  2. 读书笔记 - 《毛X东传》

    这个书名重复太多,这本的作者是迪克威尔逊.这本书很有意思,可以看出是一个局外人根据残缺不全的资料所写的出来的,而且是结合心理分析的手法主要描述政治历程.总体来说作为传记不够全面,但对于一个中国人来说可 ...

  3. python 和 C# DES加密

    C# code: using System; using System.IO; using System.Security.Cryptography; using System.Text; names ...

  4. SQLServer连接查询之Cross Apply和Outer Apply的区别及用法

    https://blog.csdn.net/wikey_zhang/article/details/77480118 先简单了解下cross apply的语法以及会产生什么样的结果集吧! 示例表: S ...

  5. 一文彻底明白linux中的selinux到底是什么

    https://www.phpyuan.com/235739.html 一.前言 安全增强型 Linux(Security-Enhanced Linux)简称 SELinux,它是一个 Linux 内 ...

  6. sf01_什么是数据结构

    数据结构解决什么问题 如何在计算机中存储数据和信息,采用什么样的方法和技巧加工处理这些数据,都是数据结构需要努力解决的问题. 解决问题的步骤 使用计算机解决问题的步骤:分析具体问题得到数学模型,设计解 ...

  7. VSCode创建自定义用户片段

    1.选择相应的用户片段类型(以"Java"为例) 首选项 -> 用户代码片段 -> java 2.设置模板 prefix 触发快捷提示的字符串前缀 body 代码片段主 ...

  8. python函数基础学习

    函数的定义与调用: def 函数名(参数1,参数2): ‘’’函数注释’’’ print(‘函数体’) return 返回值 定  义:def关键字开关,空格之后接函数名和圆括号,最后冒号结尾 def ...

  9. g++ 出现 undefined reference to ......

    g++ 出现 undefined reference to ...... 检查/usr/local/lib  /usr/lib 发现已经存在相应的库文件 那么,问题可能出现在g++链接次序上,即先链接 ...

  10. vue-cli构建项目在index.html中使用静态文件

    在vue-cli构建的项目中,且使用在移动端,我们希望每一个页面加载时都可以使用flexible.js来适配手机. 那么这个flexible.js被import到每一个组件中就不合适了. 好的方法是直 ...