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 // //采用结构 ...
随机推荐
- 1001 害死人不偿命的(3n+1)猜想 (15)(15 分)
卡拉兹(Callatz)猜想: 对任何一个自然数n,如果它是偶数,那么把它砍掉一半:如果它是奇数,那么把(3n+1)砍掉一半.这样一直反复砍下去,最后一定在某一步得到n=1.卡拉兹在1950年的世界数 ...
- day_12 内置函数
1. 内置函数 1.双下划线方法的使用 1.原来it=lst.__iter__() print(it__next__()) 2.现在it=iter(list) print(next(it)) 2. ...
- C语言一些易混淆的概念
①数组指针和指针数组 1. 数组指针用于指向一个数组,数组名是数组首元素的地址(数组名为数组首元素类型且指向首元素的指针,如int array[5] ,array为指向array[0]的指针且类型为i ...
- etcd介绍
etcd是一个开源的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现. etcd与zookeeper相比算是轻量级系统.etcd的raft比zookeeper的paxos简单. 我们用et ...
- Python 内置的GUI库tkinter方法在py2和py3中的更改
参考资料: https://docs.python.org/3.4/library/tkinter.html#tkinter-moduleshttps://docs.python.org/2.7/ ...
- Linpack之HPL测试
平台信息 Description: CentOS Linux release 7.6.1810 (Core) 注意事项 安装HPL之前需要配置好: GCC/Fortran77 编译器 BLAS/CBL ...
- (转)总结Linux的chattr与lsattr命令详解
PS:有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的li ...
- POJ 3522 ——Slim Span——————【最小生成树、最大边与最小边最小】
Slim Span Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7102 Accepted: 3761 Descrip ...
- HDU5366——The mook jong——dp
The mook jong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- Windows窗体应用开发2--窗体和控件
1.Windows窗体应用程序的各种组件 2.windows窗体控件的主要类别和功能 3.Windows窗体应用程序处理事件的方法 4.添加并配置Windows窗体和控件 5.创建时间处理程序并监视程 ...