关于优先队列浅析(priority_queue)
优先队列 greater与less,
自定义还有结构体(可以设置2层优先级) 模板;
下面废话不多说直接上程序
注释的很明白
#include<iostream>
#include<cstring>
#include<algorithm>
#include<functional>
#define nn 100000005
#include<cstdio>
#include<queue>
using namespace std;
queue<int > q;
struct nood
{
int a,b;
} p[nn],tmp;
bool operator<(nood x,nood y)
{
return x.a>y.a;
}
/*
bool operator<(int a,int b)
{
return a>b;
}
priority_queue<int > qqq;
//自定义 优先级
*/
// greater 小到大 less 大到小
//q.pop() 弹出最优元素 q.push()送入元素
//q.empty()判断队列是否为空
//q.size() 返回队列的长度
priority_queue<int,vector<int>,greater<int> > qq;
priority_queue<int,vector<int>,less<int> >qw;
int main()
{
int a[]= {,,,}; for(int i=; i<; i++)
qq.push(a[i]); for(int i=; i<; i++)
{
cout<<qq.top()<<endl;
qq.pop();
} for(int i=; i<; i++)
qw.push(a[i]);
cout<<endl;
cout<<qw.size()<<endl;
/* for(int i=0; i<4; i++)
{
cout<<qw.top()<<endl;
qw.pop();
} */
cout<<endl;
while(!qw.empty())
{
cout<<qw.top()<<endl;
qw.pop();
}
cout<<endl<<qw.size()<<endl; //结构体优先级排序
for(int i=; i<; i++)
p[i].a=i;
priority_queue<nood> stru;
for(int i=; i<; i++)
stru.push(p[i]);
cout<<endl;
cout<<stru.size()<<endl;
cout<<endl;
for(int i=; i<; i++)
{
tmp=stru.top();
cout<<tmp.a<<" ";
stru.pop();
}
cout<<endl<<stru.size()<<endl; /*priority_queue<nood,vector<nood>,greater<nood> > nod;
for(int i=0; i<5; i++)
nod.push(p[i].a);
cout<<endl;
for(int i=0; i<5; i++)
cout<<nod.top()<<" ";
cout<<nod.size()<<endl;
for(int i=0; i<5; i++)
nod.pop();
cout<<endl<<nod.size()<<endl;*/
return ;
}
关于优先队列浅析(priority_queue)的更多相关文章
- STL之优先队列(priority_queue)
转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...
- 【C语言】zz优先队列的实现
做一个题目时,看见解法中使用了优先队列,http://hawstein.com/posts/3.6.html . 颇为好奇,找资料学习了一下,顺便做个摘要. c++的用法: 转自:http://blo ...
- HDU1896Stones(优先队列)
地址http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把 ...
- 第6章 堆排序,d叉堆,优先队列
#include<stdio.h> #include<stdlib.h> #include<string.h> #define leftChild(i) (2*(i ...
- deque,list,queue,priority_queue
1.deque deque双端队列容器与vector一样,采用线性表顺序存储结构,但与vector唯一不同的是,deque采用分块的线性存储结构来存 储数据,每块的大小一般为512字节,称为一个deq ...
- 关于栈、队列、优先队列的应用——UVa11995
这本来是上一篇博客里的内容,但不知道什么原因breakdown了……我就简单放上一道题好了 题意:这道题的题目是“猜猜数据结构”,题意就是给你一些输入输出数据,让你根据这些数据判断是什么数据结构.要猜 ...
- Dijkstra+优先队列
/* Dijkstra的算法思想: 在所有没有访问过的结点中选出dis(s,x)值最小的x 对从x出发的所有边(x,y),更新 dis(s,y)=min(dis(s,y),dis(s,x)+dis(x ...
- 优先队列重载运算符< 以及初始化列表
优先队列定义 priority_queue<int, vector<int>, greater<int> >pq; 优先队列重载<运算符 在结构体中定义一个 ...
- poj 3253 Fence Repair(priority_queue)
Fence Repair Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 40465 Accepted: 13229 De ...
随机推荐
- bootstrap弹窗、弹出层、modal
bootstrap弹窗.弹出层.modal 引入bootstrap的js文件 如下div代码 <div class="modal fade" id="myMo ...
- 转载:使用Tornado+Redis维护ADSL拨号服务器代理池
我们尝试维护过一个免费的代理池,但是代理池效果用过就知道了,毕竟里面有大量免费代理,虽然这些代理是可用的,但是既然我们能刷到这个免费代理,别人也能呀,所以就导致这个代理同时被很多人使用来抓取网站,所以 ...
- yum clear all无反应
卸载重装yum 操作系统版本:centos7 [root@linux-node3 ~]# uname -r 3.10.0-514.el7.x86_64 一.将现有的yum源卸载 [root@linux ...
- Allegro导入PADS文件
1.打开要转换的PADS文件,并导出ASC文件 全选.格式选择PowerPCB V5.0 2.打开Allegro软件,导入上面生成的ASC文件 单击Translate即可完成转换,目录切换到输出路径, ...
- World Finals 2018 感想
垃圾比赛卡我读入卡我输出毁我青春.
- 阿里云 Windows 2012 如果安装IIS
用可视化安装如果出错, 那就用 windows powershell 安装 https://help.aliyun.com/knowledge_detail/40959.html?spm=5176.1 ...
- mysql Navicat 导入导出
1.导出数据库: 打开Navicat ,在我们要导出的数据库上右击鼠标,然后弹出的快捷菜单上点击“转储SQL 文件”,(有些版本, 会有子菜单,在再次弹出的子菜单项中选择第一个“数据跟结构”) ...
- (54)Wangdao.com第七天_JavaScript 运算符
JavaScript 运算符,也叫操作符 对一个或者多个值进行运算,都是会返回结果的. 比如: typeof 返回一个值的类型,返回值类型是字符串. 隐式类型转换: 任意值 = 任意值 + &q ...
- ECMA Script 6_symbol(symbol.iterator) 新接口_iterator接口
iterator 接口 只要部署了 iterator 接口 symbol(symbol.iterator), 则可以进行 for...of 遍历
- 问题:怎么把mysql的系统时间调整为电脑的时间?(已解决)
我的mysql是5.7版本. 浏览mysql的错误日志的时候,发现时间和电脑时间不一致. 查了一下,知道这个时间和log_timestamps有关, 就在mysql里执行下面一句话: SET GLOB ...