标准模板库(STL)学习指南之priority_queue优先队列

简介:

转载自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.           
  10.     public:  
  11.         void push( int t ){   
  12.             data.push_back(t);   
  13.             push_heap( data.begin(), data.end());   
  14.         }  
  15.           
  16.         void pop(){  
  17.             pop_heap( data.begin(), data.end() );  
  18.             data.pop_back();  
  19.         }  
  20.           
  21.         int top()    { return data.front(); }  
  22.         int size()   { return data.size();  }  
  23.         bool empty() { return data.empty(); }  
  24. };  
  25.   
  26. int main()  
  27. {  
  28.     priority_queue  test;  
  29.     test.push( 3 );  
  30.     test.push( 5 );  
  31.     test.push( 2 );  
  32.     test.push( 4 );  
  33.       
  34.     while( !test.empty() ){  
  35.         cout << test.top() << endl;  
  36.         test.pop(); }  
  37.           
  38.     return 0;  
  39. }  

 

 

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.       
  7.     for( int i= 0; i< 10; ++i ) q.push( rand() );  
  8.     while( !q.empty() ){  
  9.         cout << q.top() << endl;  
  10.         q.pop();  
  11.     }  
  12.       
  13.     getchar();  
  14.     return 0;  
  15. }  
 
 
如果要用到小顶堆,则一般要把模板的三个参数都带进去。
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.       
  7.     for( int i= 0; i< 10; ++i ) q.push( rand() );  
  8.     while( !q.empty() ){  
  9.         cout << q.top() << endl;  
  10.         q.pop();  
  11.     }  
  12.       
  13.     getchar();  
  14.     return 0;  
  15. }  
 
 
对于自定义类型,则必须自己重载 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.       
  16.     for( int i= 0; i< 10; ++i )  
  17.     q.push( Node( rand(), rand() ) );  
  18.       
  19.     while( !q.empty() ){  
  20.         cout << q.top().x << ' ' << q.top().y << endl;  
  21.         q.pop();  
  22.     }  
  23.       
  24.     getchar();  
  25.     return 0;  
  26. }  
 
 

自定义类型重载 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.           
  13.         return a.x> b.x; }  
  14. };  
  15. int main(){  
  16.     priority_queue<Node, vector<Node>, cmp> q;  
  17.       
  18.     for( int i= 0; i< 10; ++i )  
  19.     q.push( Node( rand(), rand() ) );  
  20.       
  21.     while( !q.empty() ){  
  22.         cout << q.top().x << ' ' << q.top().y << endl;  
  23.         q.pop();  
  24.     }  
  25.       
  26.     getchar();  
  27.     return 0;  
  28. }  

 

目录
相关文章
|
6月前
|
算法 调度 C++
【C++STL基础入门】queue基础使用
【C++STL基础入门】queue基础使用
|
1月前
|
存储 算法 C语言
【C++入门到精通】C++入门 —— priority_queue(STL)优先队列
本文介绍了C++的STL组件`std::priority_queue`,它是一个容器适配器,实现优先队列数据结构,通常基于堆实现。文章讨论了优先队列的基本概念、特点和底层堆结构,强调了其自动排序和优先级最高元素的访问。还展示了如何定义、插入、访问和移除元素,以及自定义比较函数。此外,提供了模拟实现`priority_queue`的代码示例,探讨了仿函数的作用,包括默认的`std::less`和自定义比较函数。文章鼓励读者进一步探索C++的优先队列及其应用。
26 3
|
3月前
|
容器
STL_queue
STL_queue
17 1
|
13天前
|
存储 算法 C++
【C++初阶】STL详解(九) priority_queue的使用与模拟实现
【C++初阶】STL详解(九) priority_queue的使用与模拟实现
20 0
|
13天前
|
C++ 容器
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
【C++初阶】STL详解(六)Stack与Queue的介绍与使用
18 1
|
28天前
|
存储 算法 调度
【C/C++ 数据结构 优先队列】了解学习`std::priority_queue`的使用
【C/C++ 数据结构 优先队列】了解学习`std::priority_queue`的使用
42 3
|
4月前
|
存储 C++ 容器
C++ STL学习之【优先级队列】
C++ STL学习之【优先级队列】
37 0
|
6月前
|
存储 算法 C++
『C++ - STL』之优先级队列( priority_queue )
『C++ - STL』之优先级队列( priority_queue )
|
10月前
|
存储 算法 C++
C++【STL】之priority_queue学习
C++ STL 优先级队列,常用接口的使用和模拟实现详细讲解,干货满满!
68 1
C++【STL】之priority_queue学习
|
10月前
|
设计模式 C++ 容器
C++【STL】之stack和queue学习
C++ STL stack和queue常用接口和模拟实现详细讲解,干货满满!
83 0
C++【STL】之stack和queue学习