c++ priority_queue
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的更多相关文章
- C++ std::priority_queue
std::priority_queue template <class T, class Container = vector<T>, class Compare = less< ...
- 【转载】STL之priority_queue
参考资料:传送门先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对象被放置在尾部,下一个被取出的元素则取自队列的首部.priority_queue特别之处在于,允许用户为队列中存储的元素 ...
- STL之priority_queue
下面以 long long 型队列介绍: Q.empty() // 判断队列是否为空 返回ture表示空 返回false表示空 bool Q.top() // 返回顶端元素的值 元素还在队列里 lon ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- STL之容器适配器priority_queue
priority_queue(优先队列)是一个拥有权值观念的queue,它允许加入新元素,删除旧元素,审视元素值等功能.由于这是一个queue,所以只允许在底端加入元素,并从顶端取出元素, 除此之外别 ...
- priority_queue 示例
http://www.cplusplus.com/reference/queue/priority_queue/ priority_queue 的top始终保持着为一堆数据中的最大元素. 读取最小 O ...
- 优先队列priority_queue的比较函数
STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...
- 5.1 stack,queue以及priority_queue
*:stack 使用要包含头文件stack,栈是一种先进后出的元素序列,删除和访问只能对栈顶的元素(最后一个添加的元素)进行,并且添加元素只能添加到栈顶.栈内的元素不能访问,要想访问先要删除其上方的所 ...
- 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 ...
- priority_queue 优先队列用法
//采用默认优先关系: //(priority_queue<int>que;) //Queue 0: // 91 83 72 56 47 36 22 14 10 7 3 // //采用结构 ...
随机推荐
- 转——.ashx文件与.ashx.cs
作者:PBDragon 原文连接:http://www.cnblogs.com/PBDragon/p/3811831.html 如果项目是“新建网站”,添加的ashx是没有ashx.cs的:如果是新建 ...
- CodeForces - 476B -Dreamoon and WiFi(DFS+概率思维)
Dreamoon is standing at the position 0 on a number line. Drazil is sending a list of commands throug ...
- Dijkstra算法图文详解
Dijkstra算法 Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不 ...
- input只能输入非负数
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Go语言基础之19--web编程基础
一.web编程基础 1.1 web工作方式 1.2 HTTP协议详解 a.http 请求包体 GET /domains/example/ HTTP/1.1 //请求行: 请求方法 请求URI HTTP ...
- 停止mysql服务
停止mysql服务windowsnet stop mysql (service mysqld stop win10不能用) linux /etc/init.d/mysqld stop
- eclipse安装阿里规范模板
https://github.com/alibaba/p3c/tree/master/p3c-formatter 1.代码模板(含注释等) 2.代码格式化
- 逐行创建、读取并写入txt(matlab) && 生成文件夹里文件名的.bat文件
fidin=fopen('C:\Users\byte\Desktop\新建文件夹 (4)\tr4.txt','r'); fidout=fopen('C:\Users\byte\Desktop\新建文件 ...
- 转 Django中的Form
https://www.cnblogs.com/chenchao1990/p/5284237.html Form 一.使用Form Django中的Form使用时一般有两种功能: 1.生成html标签 ...
- Android NDK开发 图片处理(五)
做过Java的同学可能经常会遇到一些关于图片处理的 例如类似QQ离线头像显示灰的.最快的算法是用colorMatrix来实现.这里通过Java调用JNI来处理每一个像素来实现. 对每一个像素点取出RG ...