关于优先队列浅析(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 ...
随机推荐
- Centos系统中彻底删除Mysql数据库
步骤: 1.输入命令查询系统中已安装的mysql. rpm -qa |grep -i mysql 2.逐个卸载mysql. yum remove 系统显示已安装的mysql 比如:yum remove ...
- Redis数据结构之intset
本文及后续文章,Redis版本均是v3.2.8 上篇文章<Redis数据结构之robj>,我们说到redis object数据结构,其有5中数据类型:OBJ_STRING,OBJ_LIST ...
- Selenium的webdriver的常用方法,鼠标事件
就来认识 WebDriver 中最常用的几个方法: get():跳转到的地址clear(): 清除文本. send_keys (value): 模拟按键输入. click(): 单击元素. 示例: f ...
- Problem J. Journey with Pigs
Problem J. Journey with Pigshttp://codeforces.com/gym/241680/problem/J考察排序不等式算出来单位重量在每个村庄的收益,然后生序排列猪 ...
- Android-XML
Android-XML XML文件: <?xml version="1.0" encoding="utf-8"?> <books> &l ...
- [Educational Round 5][Codeforces 616F. Expensive Strings]
这题调得我心疲力竭...Educational Round 5就过一段时间再发了_(:з」∠)_ 先后找了三份AC代码对拍,结果有两份都会在某些数据上出点问题...这场的数据有点水啊_(:з」∠)_[ ...
- 变量类型-Set
教程: 集合是一个无序不重复元素的序列,它的特点是:有的可变,有的不可变,元素无序不可重复! 一:集合的创建 (1)使用花括号 set={value1, values2} ...
- bitmap 合并图片
把两张bitmap覆盖合成为一张图 /** * 把两个位图覆盖合成为一个位图,以底层位图的长宽为基准 * @param backBitmap 在底部的位图 * @param frontBitmap 盖 ...
- day 25 二十五、抽象类、多态、鸭子、反射、异常处理
一.接口思想 1.接口:建立关联的桥梁,方便管理代码 python中没有接口语法 def jiao(): pass def chi(): pass def pao(): pass # 清晰知道操作的功 ...
- css页面字体替换源代码和页面显示不一样问题解决
2018年8月27日19:58:12 css指定字体的时候,可以自制的字体,比如字符替换,比如 0没有被替换,其他在源代码被替换对应的字母,但是现实的时候在替换成正确的数字 很简单,防爬虫,但是... ...