STL优先队列的使用
STL中有一个优先队列的容器可以使用。
【头文件】
queue 队列容器
vector 向量容器
【操作】
| 优先级队列支持的操作 |
|
q.empty() 如果队列为空,则返回true,否则返回false q.size() 返回队列中元素的个数 q.pop() 删除队首元素,但不返回其值 q.top() 返回具有最高优先级的元素值,但不删除该元素 q.push(item) 在基于优先级的适当位置插入新元素 |
对于Pascal留下来的手打堆习惯来说,其实对我用处不大,不过好像STL里面的复杂度更低,代码长度也能少点,以后尽量用STL好了。
#include<iostream>
#include<cstdio>
#include<queue> using namespace std; struct cmp
{
bool operator()(int x,int y)
{
return x>y;
}
}; typedef struct nod
{
int x,y;
friend bool operator < (nod a,nod b)
{
return a.y>b.y;
}
} node; int main()
{
priority_queue<int>simple;
priority_queue<int,vector<int>,cmp>define;
priority_queue<node>heap; int a[]={,,,,,,,,,}; printf("Simple Test:\n");
for (int i=;i<;i++) simple.push(a[i]);
for (int i=;i<=;i++)
{
int temp=simple.top();
simple.pop();
printf("%d\n",temp);
} printf("Define Test:\n");
for (int i=;i<;i++) define.push(a[i]);
for (int i=;i<=;i++)
{
int temp=define.top();
define.pop();
printf("%d\n",temp);
} printf("Heap Test:\n");
node b[]={{,},{,},{,},{,},{,},{,},{,},{,},{,},{,}};
for (int i=;i<;i++) heap.push(b[i]);
for (int i=;i<=;i++)
{
node temp=heap.top();
heap.pop();
printf("%d %d\n",temp.x,temp.y);
} printf("%d",<); return ;
}
STL优先队列的使用的更多相关文章
- CJOJ 2484 函数最小值 / Luogu 2085 函数最小值(STL优先队列,堆)
CJOJ 2484 函数最小值 / Luogu 2085 函数最小值(STL优先队列,堆) Description 有n个函数,分别为F1,F2,...,Fn.定义 \(Fi(x)=Aix^2+Bix ...
- CJOJ 2482 【POI2000】促销活动(STL优先队列,大根堆,小根堆)
CJOJ 2482 [POI2000]促销活动(STL优先队列,大根堆,小根堆) Description 促销活动遵守以下规则: 一个消费者 -- 想参加促销活动的消费者,在账单下记下他自己所付的费用 ...
- 基于STL优先队列和邻接表的dijkstra算法
首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...
- poj 3253 Fence Repair (STL优先队列)
版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/34805369 转载请注明出 ...
- UVA - 136 Ugly Numbers(丑数,STL优先队列+set)
Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9 ...
- 合并果子(STL优先队列)
STL优先队列:priority_queue 定义:priority_queue<int>q; 从小到大:priority_queue<int,vector<int>,g ...
- STL 优先队列
STL 栈,队列,优先队列用法 分类: Learning C++2013-11-15 00:52 843人阅读 评论(2) 收藏 举报 c++栈队列优先队列STL STL 中栈的使用方法(stack) ...
- POJ 3253 Fence Repair STL 优先队列
这题做完后觉得很水,主要的想法就是逆过程思考,原题是截断,可以想成是拼装,一共有n根木棍,最后要拼成一根完整的,每两根小的拼成一根大的,拼成后的木棍长度就是费用,要求费用最少.显然的是一共会拼接n-1 ...
- STL优先队列——踩坑日记
priority_queue 可以定义STL中的优先队列,但是优先队列在应用于自己定义的类型时需要重载<运算符,或者通过仿函数来定义比较方法,在定义比较方法的过程中,比较大的坑是STL中对于参数 ...
随机推荐
- PHP实现队列的原理
关于的队列的介绍,我这里就不多讲了,随便百度一下都很多 用过laravel框架的童鞋都知道其自带队列功能,之前我很费解,PHP只是一个脚本,有超时机制 为什么能不停的去执行队列呢? 带着这个问题,在网 ...
- windowSoftInputMode键盘把输入框挡住了
android:windowSoftInputMode="stateHidden|adjustResize" >
- git 使用系列(二)---- 分支和合并
Branching and Merging The Git feature that really makes it stand apart from nearly every other SCM o ...
- Openlayers 3 热力图
<body> <div id="map"></div> <script> var map = new ol.Map({ //初始化m ...
- php 弹窗插件
index.html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:// ...
- 初识Selenium(三)
浅谈基于Selenium的Web自动化测试框架 发表于:2011-4-25 10:58 作者:邵育亮 来源:51Testing软件测试网原创 字体:大 中 小 | 上一篇 | 下一篇 | 打印 ...
- java 方法的重载的语法规则
class People { float hello(int a,int b) { return a+b; } float hello(long a,int b) { return a-b; } do ...
- SQL Server 2012 - 内置函数
文本函数 --系统函数位置: 可编程性→函数→系统函数 -- 查询ASCII码 select ASCII('a') --查询数值对应的ASCII码 select CHAR(97) --Left . R ...
- Windows API 之 CreateThread、GetExitCodeThread(未完)
GetExitCode Retrieves the termination status of the specified thread. BOOL WINAPI GetExitCodeThread( ...
- 集合问题 离线+并查集 HDU 3938
题目大意:给你n个点,m条边,q个询问,每条边有一个val,每次询问也询问一个val,定义:这样条件的两个点(u,v),使得u->v的的价值就是所有的通路中的的最长的边最短.问满足这样的点对有几 ...