zoukankan      html  css  js  c++  java
  • Priority_queue

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

    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 
     4 using namespace std;
     5 
     6 int main(){
     7      priority_queue<int> q;
     8      
     9      for( int i= 0; i< 10; ++i ) q.push( rand() );
    10      while( !q.empty() ){
    11          cout << q.top() << endl;
    12          q.pop();
    13      }
    14      
    15      getchar();
    16      return 0;
    17 }

    对于自定义类型,则必须自己重载 operator< 或者自己写仿函数

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

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

     1 #include <iostream>
     2 #include <queue>
     3  
     4 using namespace std;
     5  
     6 struct Node{
     7     int x, y;
     8     Node( int a= 0, int b= 0 ):
     9         x(a), y(b) {}
    10 };
    11  
    12 struct cmp{
    13     bool operator() ( Node a, Node b ){
    14         if( a.x== b.x ) return a.y> b.y;
    15          
    16         return a.x> b.x; }
    17 };
    18  
    19 int main(){
    20     priority_queue<Node, vector<Node>, cmp> q;
    21      
    22     for( int i= 0; i< 10; ++i )
    23     q.push( Node( rand(), rand() ) );
    24      
    25     while( !q.empty() ){
    26         cout << q.top().x << ' ' << q.top().y << endl;
    27         q.pop();
    28     }
    29      
    30     getchar();
    31     return 0;
    32 } 
    33  
    34  
    35 //以上代码实现的是一个小顶堆

    转载:http://blog.chinaunix.net/space.php?uid=533684&do=blog&cuid=2615612

  • 相关阅读:
    zbb20181207 springboot @ConfigurationProperties使用
    zbb20181206 logback,lombok 默认日志logback配置解析
    Spring Boot (8) 全局异常处理
    Spring Boot (7) JdbcTemplate访问数据库
    Spring Boot (6) Spring Data JPA
    Spring Boot (4) 静态页面和Thymeleaf模板
    Spring Boot (3) 热部署devtools
    Spring Boot (2) Restful风格接口
    Spring Boot (1) 构建第一个Spring Boot工程
    idea使用maven搭建ssm框架实现登陆商品增删改查
  • 原文地址:https://www.cnblogs.com/zafuacm/p/3186231.html
Copyright © 2011-2022 走看看