zoukankan      html  css  js  c++  java
  • 标准模板库(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. }  
     
  • 相关阅读:
    RCNN,Fast RCNN,Faster RCNN 的前生今世:(4) SSD
    RCNN,Fast RCNN,Faster RCNN 的前生今世:(0) DMP
    nachos3.4 threads管理 (c++)
    逻辑回归与多分类逻辑回归
    [LeetCode]String to Integer (atoi)
    [LeetCode]Reverse Integer
    [LeetCode]ZigZag Conversion
    [LeetCode]Longest Palindromic Substring
    [LeetCode]Median of Two Sorted Arrays
    平面点的旋转公式
  • 原文地址:https://www.cnblogs.com/xumaojun/p/8543086.html
Copyright © 2011-2022 走看看