设为首页 加入收藏

TOP

C++中priority_queue的实现(一)
2015-07-20 17:32:58 来源: 作者: 【 】 浏览:4
Tags:priority_queue 实现

优先级队列相对于普通队列,提供了插队功能,每次最先出队的不是最先入队的元素,而是优先级最高的元素。

它的实现采用了标准库提供的heap算法。该系列算法一共提供了四个函数。使用方式如下:

首先,建立一个容器,放入元素:

vector
  
    coll;
insertNums(coll, 3, 7);
insertNums(coll, 5, 9);
insertNums(coll, 1, 4);

printElems(coll, "all elements: ");
  

打印结果为:

all elements: 
3 4 5 6 7 5 6 7 8 9 1 2 3 4

然后我们调用make_heap,这个算法把[beg, end)内的元素建立成堆

make_heap(coll.begin(), coll.end());

printElems(coll, "after make_heap: ");

打印结果:

after make_heap: 
9 8 6 7 7 5 5 3 6 4 1 2 3 4

然后我们调用pop_heap,这个算法必须保证[beg, end)已经是一个heap,然后它将堆顶的元素(其实是begin指向的元素)放到最后,再把[begin. end-1)内的元素重新调整为heap

pop_heap(coll.begin(), coll.end());
coll.pop_back();
printElems(coll, "after pop_heap: ");

打印结果为:

after pop_heap: 
8 7 6 7 4 5 5 3 6 4 1 2 3

接下来我们调用push_heap,该算法必须保证[beg, end-1)已经是一个heap,然后将整个[beg, end)调整为heap

coll.push_back(17);
push_heap(coll.begin(), coll.end());

printElems(coll, "after push_heap: ");

打印结果为:

after push_heap: 
17 7 8 7 4 5 6 3 6 4 1 2 3 5

最后我们使用sort_heap将[beg, end)由heap转化为有序序列,所以,前提是[beg, end)已经是一个heap

sort_heap(coll.begin(), coll.end());
printElems(coll, "after sort_heap: ");

打印结果为:

after sort_heap: 
1 2 3 3 4 4 5 5 6 6 7 7 8 17

完整的测试代码如下:

复制代码
#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; template 
      
        void insertNums(T &t, int beg, int end) { while(beg <= end) { t.insert(t.end(), beg); ++beg; } } template 
       
         void printElems(const T &t, const string &s = "") { cout << s << endl; for(typename T::const_iterator it = t.begin(); it != t.end(); ++it) { cout << *it << " "; } cout << endl; } int main(int argc, char const *argv[]) { vector
        
          coll; insertNums(coll, 3, 7); insertNums(coll, 5, 9); insertNums(coll, 1, 4); printElems(coll, "all elements: "); //在这个范围内构造heap make_heap(coll.begin(), coll.end()); printElems(coll, "after make_heap: "); //将堆首放到最后一个位置,其余位置调整成堆 pop_heap(coll.begin(), coll.end()); coll.pop_back(); printElems(coll, "after pop_heap: "); coll.push_back(17); push_heap(coll.begin(), coll.end()); printElems(coll, "after push_heap: "); sort_heap(coll.begin(), coll.end()); printElems(coll, "after sort_heap: "); return 0; }
        
       
      
     
    
   
  
复制代码

根据以上的算法,我们来实现标准库的优先级队列priority_queue,代码如下:

复制代码
#ifndef PRIORITY_QUEUE_HPP
#define PRIORITY_QUEUE_HPP

#include 
  
   
#include 
   
     #include 
    
      template 
     
      , typename Compare = std::less
      
        > class PriorityQueue { public: typedef typename Container::value_type value_type; //不用T typedef typename Container::size_type size_type; typedef Container container_type; typedef value_type &reference; typedef const value_type &const_reference; PriorityQueue(const Compare& comp = Compare(), const Container& ctnr = Container()); template 
       
         PriorityQueue (InputIterator first, InputIterator last, const Compare& comp = Compare(), const Container& ctnr = Container()); void push(const value_type &val) { cont_.push_back(val); //调整最后一个元素入堆 std::push_heap(cont_.begin(), cont_.end(), comp_); } void pop() { //第一个元素移出堆,放在最后 std::pop_heap(cont_.begin(), cont_.end(), comp_); cont_.pop_back(); } bool empty() const { return cont_.empty(); } size_type size() const { return cont_.size(); } const_reference top() const { return cont_.front(); } private: Compare comp_; //比较规则 Container cont_; //内部容器 }; template 
        
          PriorityQueue
         
          ::PriorityQueue(const Compare& comp, const Container& ctnr) :comp_(comp), cont_(ctnr) { std::make_heap(cont_.begin(), cont_.end(), comp_); //建堆 } template 
          
            template 
           
             PriorityQueue
            
             ::PriorityQueue (InputIterator first, InputIterator last, const Compare& comp, const Container& ctnr) :comp_(comp), cont_(ctnr) { cont_.insert(cont_.end(), first, last); std::make_heap(cont_.begin(), cont_.end(), c
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇HDOJ 4251 The Famous ICPC Team .. 下一篇HDOJ 4417 Super Mario

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·JAVA现在的就业环境 (2025-12-26 01:19:24)
·最好的java反编译工 (2025-12-26 01:19:21)
·预测一下2025年Java (2025-12-26 01:19:19)
·Libevent C++ 高并发 (2025-12-26 00:49:30)
·C++ dll 设计接口时 (2025-12-26 00:49:28)