模板:优先队列(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不同的就在于我们可以自定义其中数据的优先级, 让优先级高的排在队列前面,优先出队 优先队列具有队列的所有特性, ...
随机推荐
- (Step by Step)How to setup IP Phone Server(VoIP Server) for free.
You must have heard about IP Phone and SIP (Software IP Phone).Nowadays standard PSTN phone are bein ...
- Learning JavaScript Design Patterns The Singleton Pattern
The Singleton Pattern The Singleton pattern is thus known because it restricts instantiation of a cl ...
- The Dangers of JavaScript’s Automatic Semicolon Insertion
Although JavaScript is very powerful, the language’s fundamentals do not have a very steep learning ...
- css3随笔
1 rgba和opacity的区别 RGBA即红色R+绿色G+蓝色B+通道Alpha 语法: R:红色值.正整数 | 百分数 G:绿色值.正整数 | 百分数 B:蓝色值.正整数| 百分数 A:透明度. ...
- zabbix邮件报警脚本
#!/usr/bin/python #coding:utf-8 import smtplib from email.mime.text import MIMEText import sys mail_ ...
- IOS ScrollowView 滑动到边缘后不允许再拖动
当scrollowview滑动图片时,滑动到最后一张图本应该不让其滑动,但是如果不可以去设置属性,依然可以滑动,露出白色的底色,挺影响美观的, 可以设置其属性: sv.bounces=NO; 这样就不 ...
- 生成N个不相等的随机数
近期项目中须要生成N个不相等的随机数.实现的时候.赶工期,又有项目中N非常小(0-100)直接谢了一个最直观的方法: public static List<Integer> randomS ...
- leetcode每日解题思路 221 Maximal Square
问题描述: 题目链接:221 Maximal Square 问题找解决的是给出一个M*N的矩阵, 只有'1', '0',两种元素: 需要你从中找出 由'1'组成的最大正方形.恩, 就是这样. 我们看到 ...
- 从源码角度深入理解Toast
Toast这个东西我们在开发中经常用到,使用也很简单,一行代码就能搞定: 1: Toast.makeText(", Toast.LENGTH_LONG).show(); 但是我们经常会遇到这 ...
- AWS ElastiCache 使用笔记
使用 AWS 管理控制台创建 Redis 缓存集群 创建子网组 在 Amazon VPC 中创建集群,则您必须指定缓存子网组.ElastiCache 使用该缓存子网组选择一个子网和此子网内的 IP 地 ...