优先队列 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)的更多相关文章

  1. STL之优先队列(priority_queue)

    转自网上大牛博客,原文地址:http://www.cnblogs.com/summerRQ/articles/2470130.html 先回顾队列的定义:队列(queue)维护了一组对象,进入队列的对 ...

  2. 【C语言】zz优先队列的实现

    做一个题目时,看见解法中使用了优先队列,http://hawstein.com/posts/3.6.html . 颇为好奇,找资料学习了一下,顺便做个摘要. c++的用法: 转自:http://blo ...

  3. HDU1896Stones(优先队列)

    地址http://acm.hdu.edu.cn/showproblem.php?pid=1896 题目大一比较简单,就是说在一条直线道路上有n个石头,往前走,遇到一个数一个,如果遇到的是第奇数个那就把 ...

  4. 第6章 堆排序,d叉堆,优先队列

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define leftChild(i) (2*(i ...

  5. deque,list,queue,priority_queue

    1.deque deque双端队列容器与vector一样,采用线性表顺序存储结构,但与vector唯一不同的是,deque采用分块的线性存储结构来存 储数据,每块的大小一般为512字节,称为一个deq ...

  6. 关于栈、队列、优先队列的应用——UVa11995

    这本来是上一篇博客里的内容,但不知道什么原因breakdown了……我就简单放上一道题好了 题意:这道题的题目是“猜猜数据结构”,题意就是给你一些输入输出数据,让你根据这些数据判断是什么数据结构.要猜 ...

  7. Dijkstra+优先队列

    /* Dijkstra的算法思想: 在所有没有访问过的结点中选出dis(s,x)值最小的x 对从x出发的所有边(x,y),更新 dis(s,y)=min(dis(s,y),dis(s,x)+dis(x ...

  8. 优先队列重载运算符< 以及初始化列表

    优先队列定义 priority_queue<int, vector<int>, greater<int> >pq; 优先队列重载<运算符 在结构体中定义一个 ...

  9. poj 3253 Fence Repair(priority_queue)

    Fence Repair Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 40465   Accepted: 13229 De ...

随机推荐

  1. ABP使用Miniprofiler监测EF

    在上篇教程中,我们在WebApi项目中集成了Miniprofiler,本篇文章中,将继续集成Miniprofiler EF6,以实时监测分析EF的执行语句.执行效率等.Miniprofiler会针对E ...

  2. 03.Regression

    01.regression # -*- coding: utf-8 -*- """ scipy 패키지 선형 회귀분석 """ from s ...

  3. Centos7/RHEL7 开启kdump

    Kdump是一种基于kexec的Linux内核崩溃捕获机制,简单来说系统启动时会预留一块内存,当系统崩溃调用命令kexec(kdump kernel)在预留的内存中启动kdump内核, 该内核会将此时 ...

  4. Innodb与Myisam引擎的区别与应用场景

    1. 区别: (1)事务处理: MyISAM是非事务安全型的,而InnoDB是事务安全型的(支持事务处理等高级处理): (2)锁机制不同: MyISAM是表级锁,而InnoDB是行级锁: (3)sel ...

  5. vue-cli 构建的项目中 如何使用less

    vue-cli 构建的项目默认是不支持 less 的,需要自己添加. 首选,安装 less 和 less-loader ,在项目目录下运行如下命令 npm install less less-load ...

  6. spark rdd df dataset

    RDD.DataFrame.DataSet的区别和联系 共性: 1)都是spark中得弹性分布式数据集,轻量级 2)都是惰性机制,延迟计算 3)根据内存情况,自动缓存,加快计算速度 4)都有parti ...

  7. Elasticsearch大规模时序索引如何治理和规划

    什么是时序索引? 其主要特点体现在两个方面, 一存,以时间为轴,数据只有增加,没有变更,并且必须包含timestamp(日期时间,名称随意)字段,其作用和意义要大于数据的id字段,常见的数据比如我们通 ...

  8. 程序一 用记事本建立文件src.dat,其中存放若干字符。编写程序,从文件src.dat中读取数据,统计其中的大写字母、小写字母、数字、其它字符的个数,并将这些数据写入到文件test.dat中。

    用记事本建立文件src.dat,其中存放若干字符.编写程序,从文件src.dat中读取数据,统计其中的大写字母.小写字母.数字.其它字符的个数,并将这些数据写入到文件test.dat中. #inclu ...

  9. NEERC-2017

    A. Archery Tournament 用线段树套set维护横坐标区间内的所有圆,查询时在$O(\log n)$个set中二分查找即可. 时间复杂度$O(n\log^2n)$. #include& ...

  10. 报错!!!Servlet.service() for servlet [action] in context with path [/myssh] threw exception [java.lang.NullPointerException] with root cause java.lang.NullPointerException

    这个为什么报错啊~~ at com.hsp.basic.BasicService.executeQuery(BasicService.java:33) 这个对应的语句是   Query query = ...