转载自CSDN博客:http://blog.csdn.net/suwei19870312/article/details/5294016

priority_queue 调用 STL里面的 make_heap(), pop_heap(), push_heap() 算法
实现,也算是堆的另外一种形式。

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

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. using namespace std;
  5. class priority_queue
  6. {
  7. private:
  8. vector<int> data;
  9. public:
  10. void push( int t ){
  11. data.push_back(t);
  12. push_heap( data.begin(), data.end());
  13. }
  14. void pop(){
  15. pop_heap( data.begin(), data.end() );
  16. data.pop_back();
  17. }
  18. int top()    { return data.front(); }
  19. int size()   { return data.size();  }
  20. bool empty() { return data.empty(); }
  21. };
  22. int main()
  23. {
  24. priority_queue  test;
  25. test.push( 3 );
  26. test.push( 5 );
  27. test.push( 2 );
  28. test.push( 4 );
  29. while( !test.empty() ){
  30. cout << test.top() << endl;
  31. test.pop(); }
  32. return 0;
  33. }

STL里面的 priority_queue 写法与此相似,只是增加了模板及相关的迭代器什么的。

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

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. int main(){
  5. priority_queue<int> q;
  6. for( int i= 0; i< 10; ++i ) q.push( rand() );
  7. while( !q.empty() ){
  8. cout << q.top() << endl;
  9. q.pop();
  10. }
  11. getchar();
  12. return 0;
  13. }
 
如果要用到小顶堆,则一般要把模板的三个参数都带进去。
STL里面定义了一个仿函数 greater<>,对于基本类型可以用这个仿函数声明小顶堆
例子:
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. int main(){
  5. priority_queue<int, vector<int>, greater<int> > q;
  6. for( int i= 0; i< 10; ++i ) q.push( rand() );
  7. while( !q.empty() ){
  8. cout << q.top() << endl;
  9. q.pop();
  10. }
  11. getchar();
  12. return 0;
  13. }
 
对于自定义类型,则必须自己重载 operator< 或者自己写仿函数
先看看例子:
  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. struct Node{
  5. int x, y;
  6. Node( int a= 0, int b= 0 ):
  7. x(a), y(b) {}
  8. };
  9. bool operator<( Node a, Node b ){
  10. if( a.x== b.x ) return a.y> b.y;
  11. return a.x> b.x;
  12. }
  13. int main(){
  14. priority_queue<Node> q;
  15. for( int i= 0; i< 10; ++i )
  16. q.push( Node( rand(), rand() ) );
  17. while( !q.empty() ){
  18. cout << q.top().x << ' ' << q.top().y << endl;
  19. q.pop();
  20. }
  21. getchar();
  22. return 0;
  23. }
 

自定义类型重载 operator< 后,声明对象时就可以只带一个模板参数。
但此时不能像基本类型这样声明
priority_queue<Node, vector<Node>, greater<Node> >;
原因是 greater<Node> 没有定义,如果想用这种方法定义
则可以按如下方式

例子:

  1. #include <iostream>
  2. #include <queue>
  3. using namespace std;
  4. struct Node{
  5. int x, y;
  6. Node( int a= 0, int b= 0 ):
  7. x(a), y(b) {}
  8. };
  9. struct cmp{
  10. bool operator() ( Node a, Node b ){
  11. if( a.x== b.x ) return a.y> b.y;
  12. return a.x> b.x; }
  13. };
  14. int main(){
  15. priority_queue<Node, vector<Node>, cmp> q;
  16. for( int i= 0; i< 10; ++i )
  17. q.push( Node( rand(), rand() ) );
  18. while( !q.empty() ){
  19. cout << q.top().x << ' ' << q.top().y << endl;
  20. q.pop();
  21. }
  22. getchar();
  23. return 0;
  24. }
 

标准模板库(STL)学习指南之priority_queue优先队列的更多相关文章

  1. 标准模板库(STL)学习指南之sort排序

    对于程序员来说,数据结构是必修的一门课.从查找到排序,从链表到二叉树,几乎所有的算法和原理都需要理解,理解不了也要死记硬背下来.幸运的是这些理论都已经比较成熟,算法也基本固定下来,不需要你再去花费心思 ...

  2. 标准模板库(STL)学习指南之List链表

    本文转载自天极网,原文地址:http://www.yesky.com/255/1910755.shtml.转载请注明 什么是STL呢?STL就是Standard Template Library,标准 ...

  3. 标准模板库(STL)学习指南之set集合

    set是关联容器.其键值就是实值,实值就是键值,不可以有重复,所以我们不能通过set的迭代器来改变set的元素的值,set拥有和list相同的特性:当对他进行插入和删除操作的时候,操作之前的迭代器依然 ...

  4. 标准模板库(STL)学习指南之map映射

    转载自CSDN博客:http://blog.csdn.net/bat603/article/details/1456141 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关 ...

  5. 标准模板库(STL)学习指南之vector向量

    vector – 一.  vector可以模拟动态数组 – 二.  vector的元素可以是任意类型T,但必须具备赋值和拷贝能力(具有public 拷贝构造函数和重载的赋值操作符) 三.必须包含的头文 ...

  6. 标准模板库(STL)学习探究之stack

    标准模板库(STL)学习探究之stack queue priority_queue list map/multimap dequeue string

  7. 标准模板库(STL)学习探究之vector容器

    标准模板库(STL)学习探究之vector容器  C++ Vectors vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库.vector之所以被 ...

  8. 标准模板库(STL)学习探究之Multimap容器

    C++ Multimaps和maps很相似,但是MultiMaps允许重复的元素.(具体用法请参考map容器)     函数列表:     begin() 返回指向第一个元素的迭代器      cle ...

  9. STL学习系列之一——标准模板库STL介绍

    库是一系列程序组件的集合,他们可以在不同的程序中重复使用.C++语言按照传统的习惯,提供了由各种各样的函数组成的库,用于完成诸如输入/输出.数学计算等功能. 1. STL介绍 标准模板库STL是当今每 ...

随机推荐

  1. mysql时间相减的问题

    MySQL中时间不能直接相减,如果日.分.时不同,相减结果是错误的 mysql> select t1,t2,t2-t1 from mytest;   +--------------------- ...

  2. Asp.Net MVC3中如何进行单元测试?

    下面我们就以一个示例演示一下如何进行单元测试? public Model.UserInfo UpdateEntity(Model.UserInfo entity) { db.UserInfo.Atta ...

  3. cocoapods最新使用

    1.首先用淘宝的Ruby镜像来访问CocoaPods,打开终端输入以下命令: (1)gem sources --remove http://ruby.gems.org/   (移除现有Ruby默认源) ...

  4. api 爬虫 避免相同 input 在信息未更新 情况下 重复请求重复

  5. Delphi窗体研究,留个爪,以后回来研究

    Delphi - 窗体创建过程   来自大富翁. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ...

  6. centos6.9下设置nginx服务开机自动启动

    首先,在linux系统的/etc/init.d/目录下创建nginx文件,使用如下命令: vi /etc/init.d/nginx 在脚本中添加如下命令: #!/bin/sh # # nginx - ...

  7. Java从零开始 第一天

    java是一门优秀的编程语言.java也有很多优点.从零开始.这是第一天,不是为什么,而是让自己学的更多. 1.下载JDK 在浏览器输http://www.oracle.com/index.html. ...

  8. python实例2-写一个爬虫下载小功能

    主要是通过url,和re两个模块对一个网页的固定图片进行模糊匹配后下载下来. #! /usr/bin/python import re import urllib def gethtml(url): ...

  9. java获取调用此方法的上面的方法名、类

    StackTraceElement[] stacks = (new Throwable()).getStackTrace(); for (StackTraceElement stack : stack ...

  10. C#仿QQ设置界面导航

    效果预览,选择左边标签,右边内容会自动滚动到适当位置 public class AnchorPanel { List<PanelMenu> lst = new List<PanelM ...