模板:优先队列(priority_queue)
#include <iostream>
#include <cstdio>
#include <queue>
#include <vector> using namespace std; struct node
{
int priortity;
int value; friend bool operator<(node n1,node n2)
{
return n1.priortity < n2.priortity; // < 从大到小 , > 从小到大
}
}; int main()
{
const int len = ;
int i;
int a[len] = {,,,,}; //example 1:
priority_queue<int> qi; for(i = ; i < len; ++i)
{
qi.push(a[i]);
} for(i = ; i < len; ++i)
{
cout << qi.top() << endl;
qi.pop();
} //example 2:
priority_queue<int,vector<int>,greater<int> > qi2; // greater从小到大,less从大到小 for(i = ; i < len; ++i)
{
qi2.push(a[i]);
} for(i = ; i < len; ++i)
{
cout << qi2.top() << endl;
qi2.pop();
} //example 3:
priority_queue<node> qn;
node b[len];
b[].priortity = ; b[].value = ;
b[].priortity = ; b[].value = ;
b[].priortity = ; b[].value = ;
b[].priortity = ; b[].value = ;
b[].priortity = ; b[].value = ; for(i = ; i < len; ++i)
{
qn.push(b[i]);
} for(i = ; i < len; ++i)
{
cout << qn.top().priortity << "\t" << qn.top().value << endl;
qn.pop();
} return ;
}
参考文献:
http://wenku.baidu.com/link?url=_Y5TTAR5M4O2Eakp3lckzhc9ZkvGKp0Bqk-iSgxzUQOL5kuxGwwLZlFO1cq4ZqqvJV1HoNgurOftPRJvKp9Ng3S83c43tS1tQp0jxFHBp-u
模板:优先队列(priority_queue)的更多相关文章
- 浅谈C++ STL中的优先队列(priority_queue)
从我以前的博文能看出来,我是一个队列爱好者,很多并不是一定需要用队列实现的算法我也会采用队列实现,主要是由于队列和人的直觉思维的一致性导致的. 今天讲一讲优先队列(priority_queue),实际 ...
- 优先队列priority_queue的简单应用
优先队列 引入 优先队列是一种特殊以及强大的队列. 那么优先队列是什么呢? 说白了,就是一种功能强大的队列. 它的功能强大在哪里呢? 四个字:自动排序. 优先队列的头文件&&声明 头文 ...
- 9.优先队列,priority_queue
#include <iostream> #include <queue> #include <deque> #include <list> using ...
- 892A. Greed#贪婪(优先队列priority_queue)
题目出处:http://codeforces.com/problemset/problem/892/A 题目大意:有一些可乐(不一定装满),问能不能把所有可乐装进两个可乐瓶中 #include< ...
- 标准模板库中的优先队列(priority_queue)
//C++数据结构与算法(第4版) Adam Drozdek 著 徐丹 吴伟敏<<清华大学出版社>> #include<queue> priority_queu ...
- 【STL】优先队列priority_queue详解+OpenJudge-4980拯救行动
一.关于优先队列 队列(queue)这种东西广大OIer应该都不陌生,或者说,队列都不会你还学个卵啊(╯‵□′)╯︵┻━┻咳咳,通俗讲,队列是一种只允许从前端(队头)删除元素.从后端(队尾)插入元素的 ...
- 优先队列priority_queue的比较函数
STL头文件:#include<queue> 优先队列: 默认从大到小排列:priority_queuee<node>q; 自定义优先级的三种方法: 1.重载操作符: bool ...
- C++ 优先队列priority_queue用法【转载】
priority_queue 对于基本类型的使用方法相对简单.他的模板声明带有三个参数,priority_queue<Type, Container, Functional>Type 为数 ...
- [转]c++优先队列(priority_queue)用法详解
既然是队列那么先要包含头文件#include <queue>, 他和queue不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队 优先队列具有队列的所有特性, ...
随机推荐
- Android - Shared Preference (分享首选项) 具体解释
Shared Preference (分享首选项) 具体解释 本文地址: http://blog.csdn.net/caroline_wendy/article/details/24454963 Sh ...
- APP下载页面(支持微信扫一扫)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8&quo ...
- Oracle基础学习1--Oracle安装
安装过程较简单.按着步骤走就可以.这里须要提醒假设要使用PL/SQL来操作Oracle.那么最好安装32位Oracle程序.原因是网上说PL/SQL仅仅对32位Oracle进行支持,假设用64为Ora ...
- Linux环境进程间通信
http://www.ibm.com/developerworks/cn/linux/l-ipc/part2/index2.html http://bbs.chinaunix.net/forum.ph ...
- SOAP web service用AFNetWorking实现请求
问: This is my current call to (asmx) SOAP web service: NSString *soapMessage = [NSString stringWithF ...
- Qt全局热键(windows篇)
Qt对于系统底层,一直没有很好的支持,例如串口并口通信,还有我们经常都会用到的全局热键,等等.既然Qt可能出于某种原因,不对这些进行支持,我们就只能自己写代码,调用系统相关的API了. 注意,这个 ...
- CCLabelTTF 如何支持换行符和换行
参考自http://www.cocos2d-x.org/wiki/How_does_CCLabelTTF_support_line_breaks_and_wrapping 环境: cocos2d-x ...
- 【转】最新基于adt-bundle-windows-x86的android开发环境搭建
http://blog.csdn.net/wangqiuyun/article/details/8731240 某系统要配套做一个android客户端,来一次android开发环境快速搭建,系统Win ...
- linux find命令详解--转
转自:http://blog.csdn.net/jakee304/article/details/1792830 (一)Get Start 最简单的find用法莫过于如此: $ find . 查找当前 ...
- oracle 查看表属主和表空间sql
查看表空间 select * from user_tablespaces where table_name = 'TableName' 查看表属主 select Owner from all_ta ...