关于优先队列浅析(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 ...
随机推荐
- spark MLlib Classification and regression 学习
二分类:SVMs,logistic regression,decision trees,random forests,gradient-boosted trees,naive Bayes 多分类: ...
- Linux安装Tomcat-Nginx-FastDFS-Redis-Solr-集群——【第六集之基本命令使用】
学习命令的方法:linux中所有操作都是命令操作,可想而知命令有多少,更严重的是每个命令有很多参数,记命令容易,记参数就难了,所以建议: 自己准备一个博客,把通常用到的命令及其功能记载下来,用到的时候 ...
- Problem B. Beer Refrigerator
http://codeforces.com/gym/241680/problem/B比赛的时候考虑的是,它们3个尽可能接近,然后好麻烦,不如暴力枚举,这里不需要质因数分解,而是两重循环枚举所有因数,第 ...
- chattr和lsattr的基本用法
lsattr filename 查看一个文件的属性 chattr filename 改变一个文件的属性 主要用途:实现文件的原有内容不允许改变,但可以增加新行,则需通过改变文件的属性来实现. ch ...
- python使用urllib2 http 下载参数的try catch
import urllib2 req = urllib2.Request('http://blog.csdn.net/cqcre')try: urllib2.urlopen(req)except ur ...
- 如何避免OOM
一.减小对象的内存占用 1)使用更加轻量的数据结构 例如,我们可以考虑使用ArrayMap/SparseArray而不是HashMap等传统数据结构. ArrayMap和HashMap主要不同之处在于 ...
- Buffer --缓冲器
一. 启动Buffer缓冲器 node 输入 buffer 创建一个新的buffer var buf = new buffer(''hello word) 查看buf的长度 buf.length 运行 ...
- 那些年我们跳过的 IE坑
一, IE input X 去掉文本框的叉叉和密码输入框的眼睛图标 解: 从IE 10开始,type=”text” 的 input 在用户输入内容后,会自动产生一个小叉叉(X),方便用户点击清 ...
- JS基础学习1
1 JS 概述 一个完整的javascript实现是由以下3个不同部分组成的: (1) 核心(ECMAscript) (2) 文档对象模型(DOM) Document object ...
- React 简介及其基本使用
相关概念: 插件: 别人写好的第三方库 xxx 插件: jQuery 插件 / react 插件 全家桶: 核心库 + 相关插件 构建项目: 对项目源码 进行 编译.合并.压缩......处理,生 ...