由于优先级队列的内部数据结构为 ,所以这里先介绍堆的一些操作。

堆的一些函数操作在algorithm头文件中

//在[first, last)范围内构造最大堆,first,last 可以是vector指针也可以是数组指针
make_heap(first ,last)
make_heap(first ,last, cmpObject)

默认情况下是建立最大堆,即首元素为两个地址间的最大值。默认为less<int>,可以改为greater<int>即为建立最小堆

pop_heap(first ,last)
pop_heap(first ,last, cmpObject)

将front(即第一个最大元素)移动到end的前部,同时将剩下的元素重新构造成(堆排序)一个新的heap。

push_heap(first ,last)
push_heap(first ,last, cmpObject)

对刚插入的(尾部)元素做堆排序。在进行push_heap操作之前,除了最后一个元素以外的其他元素必须构成一个最大堆,

否则会报错。

sort_heap(first ,last)
sort_heap(first ,last, cmpObject)

将一个堆做排序,最终成为一个有序的系列,可以看到sort_heap时,必须先是一个堆(两个特性:1、最大元素在第一个 2、添加或者删除元素以对数时间),因此必须先做一次make_heap.

先写一个用 STL 里面堆算法实现的与真正的STL里面的priority_queue 用法相
似的 priority_queue,以加深对priority_queue 的理解

#include <iostream>
#include <algorithm>
#include <vector> using namespace std; class priority_queue
{
private:
vector<int> data;
public:
void push( int t ){
data.push_back(t);
push_heap( data.begin(), data.end());
}
void pop(){
pop_heap( data.begin(), data.end() );
data.pop_back();
}
int top() { return data.front(); }
int size() { return data.size(); }
bool empty() { return data.empty(); }
}; int main()
{
priority_queue test;
test.push( );
test.push( );
test.push( );
test.push( );
test.push();
test.push();
while( !test.empty() ){
cout << test.top() << endl;
test.pop(); }
return ;
}

模板声明带有三个参数,priority_queue<Type, Container, Functional>
  Type 为数据类型, Container 为保存数据的容器, Functional 为元素比较方式。
  Container 必须是用数组实现的容器,比如 vector, deque 但不能用list.
  STL里面默认用的是vector. 比较方式默认用operator< , 所以如果你把后面俩个
  参数 缺省的话,优先队列就是大顶堆,队头元素最大。

下面的程序为建立一个最小堆的例子 priority_queue<int,vector<int>,greater<int>> q;

#include <iostream>
#include <queue> using namespace std; int main(){
priority_queue<int, vector<int>, greater<int> > q;
for( int i= ; i< ; ++i ) q.push( rand() );
while( !q.empty() ){
cout << q.top() << endl;
q.pop();
}
getchar();
return ;
}

自定义类型Node的优先级队列,重载比较运算符'<':

#include <iostream>
#include <queue> using namespace std; struct Node{
int x, y;
Node( int a= , int b= ):
x(a), y(b) {}
}; bool operator<( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x;
} int main(){
priority_queue<Node> q;
for( int i= ; i< ; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
getchar();
return ;
}

重载‘<’之后可以只带一个模板参数定义优先级队列对象,但不能声明priority_queue<Node, vector<Node>, greater<Node> >

这种类型,原因是greater<Node>没有定义,要想定义上述形式必须定义一个比较的类cmp,具体实现如下代码。

#include <iostream>
#include <queue> using namespace std; struct Node{
int x, y;
Node( int a= , int b= ):
x(a), y(b) {}
}; struct cmp{
bool operator() ( Node a, Node b ){
if( a.x== b.x ) return a.y> b.y;
return a.x> b.x; }
}; int main(){
priority_queue<Node, vector<Node>, cmp> q;
for( int i= ; i< ; ++i )
q.push( Node( rand(), rand() ) );
while( !q.empty() ){
cout << q.top().x << ' ' << q.top().y << endl;
q.pop();
}
getchar();
return ;
}

参考博文:

  http://blog.163.com/zjut_nizhenyang/blog/static/1695700292010101632059540/

  http://blog.csdn.net/fuyufjh/article/details/47144869

  http://blog.sina.com.cn/s/blog_959bf1d3010191h1.html

优先级队列用法详解(priority_queue)的更多相关文章

  1. STL priority_queue 常见用法详解

    <算法笔记>学习笔记 priority_queue 常见用法详解 //priority_queue又称优先队列,其底层时用堆来实现的. //在优先队列中,队首元素一定是当前队列中优先级最高 ...

  2. Vue1.0用法详解

    Vue.js 不支持 IE8 及其以下版本,因为 Vue.js 使用了 IE8 不能实现的 ECMAScript 5 特性. 开发环境部署 可参考使用 vue+webpack. 基本用法 1 2 3 ...

  3. c++中vector的用法详解

    c++中vector的用法详解 vector(向量): C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当程序员无法知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间 ...

  4. JS逗号运算符的用法详解

    逗号运算符的用法详解 注意: 一.由于目前正在功读JavaScript技术,所以这里拿JavaScript为例.你可以自己在PHP中试试. 二.JavaScript语法比较复杂,因此拿JavaScri ...

  5. 教程-Delphi中Spcomm使用属性及用法详解

    Delphi中Spcomm使用属性及用法详解 Delphi是一种具有 功能强大.简便易用和代码执行速度快等优点的可视化快速应用开发工具,它在构架企业信息系统方面发挥着越来越重要的作用,许多程序员愿意选 ...

  6. oracle正则表达式regexp_like的用法详解

    oracle正则表达式regexp_like的用法详解 /*ORACLE中的支持正则表达式的函数主要有下面四个:1,REGEXP_LIKE :与LIKE的功能相似2,REGEXP_INSTR :与IN ...

  7. lsof 命令用法详解

    lsof 命令用法详解 作用 用于查看你进程开打的文件,打开文件的进程,进程打开的端口(TCP.UDP).找回/恢复删除的文件.是十分方便的系统监视工具,因为lsof命令需要访问核心内存和各种文件,所 ...

  8. golang格式化输出-fmt包用法详解

    golang格式化输出-fmt包用法详解 注意:我在这里给出golang查询关于包的使用的地址:https://godoc.org    声明: 此片文章并非原创,大多数内容都是来自:https:// ...

  9. Elasticsearch SQL用法详解

    Elasticsearch SQL用法详解  mp.weixin.qq.com 本文详细介绍了不同版本中Elasticsearch SQL的使用方法,总结了实际中常用的方法和操作,并给出了几个具体例子 ...

随机推荐

  1. LOJ6002 - 「网络流 24 题」最小路径覆盖

    原题链接 Description 求一个DAG的最小路径覆盖,并输出一种方案. Solution 模板题啦~ Code //「网络流 24 题」最小路径覆盖 #include <cstdio&g ...

  2. SpringBoot SpringSession redis 共享 SESSION

    号称无缝整合httpsession 共享, 但注意如果存在第三方框架,例如SESSION并发控制,这个是需要自己重写session名单的. 关于redis session 共享 的session并发控 ...

  3. C语言中的sizeof函数总结

    sizeof函数的结果: 变量:变量所占的字节数. ; printf( 数组:数组所占的字节数. ,,,,}; ] = {,,,,}; printf("size_arr1=%d\n" ...

  4. AutoAudit研究学习

    AutoAudit介绍   AutoAudit这个是Paul Nielsen写的一个开源的审计跟踪的脚本项目,项目位于https://autoaudit.codeplex.com/上,Paul Nie ...

  5. HighCharts之2D折线图

    HighCharts之2D折线图 1.HighCharts之2D折线图源码 line.html: <!DOCTYPE html> <html> <head> < ...

  6. freemarker写select组件报错总结(二)

    1.错误描述 六月 25, 2014 11:32:49 下午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...

  7. Java和Flex整合报错(二)

    1.错误原因 usage: java org.apache.catalina.startup.Catalina [ -config {pathname} ] [ -nonaming ] { -help ...

  8. TypeError: Error #1034: 强制转换类型失败:无法将 "0.49" 转换为 mx.graphics.IFill。

    1.错误描述 TypeError: Error #1034: 强制转换类型失败:无法将 "0.49" 转换为 mx.graphics.IFill. at mx.charts.ser ...

  9. Alice and Bob HDU - 4268

    Alice and Bob's game never ends. Today, they introduce a new game. In this game, both of them have N ...

  10. 异常-----Can't convert the date to string, because it is not known which parts of the date variable are in use. Use ?date, ?time or ?datetime built-in, or ?string.\u003Cformat> or ?string(format) built-

    1.错误描述 五月 27, 2014 12:07:05 上午 freemarker.log.JDK14LoggerFactory$JDK14Logger error 严重: Template proc ...