• boost heap


    1. using boost::heap::priority_queue

    #include <boost/heap/priority_queue.hpp>
    #include <iostream>
    
    using namespace boost::heap;
    
    int main() {
      priority_queue<int> pq;
      pq.push(2);
      pq.push(3);
      pq.push(1);
    
      for (int i : pq) {
        std::cout << i << std::endl;
      }
    
      priority_queue<int> pq2;
      pq2.push(3);
      std::cout << std::boolalpha << (pq > pq2) << std::endl;
      return 0;
    }

    In general this class behaves like std::priority_queue, except it allows you to iterate over elements. The order of elements returned in the iteration is random.

    Objects of type boost::heap::priority_queue can be compared with each other. The comparison above returns true because pq has more elements than pq2. If both queues had the same number of elements, the elements would be compared in pairs.

    2. using boost::heap::binomial_heap

    #include <boost/heap/binomial_heap.hpp>
    #include <iostream>
    
    using namespace boost::heap;
    
    int main()
    {
      binomial_heap<int> bh;
      bh.push(2);
      bh.push(3);
      bh.push(1);
    
      binomial_heap<int> bh2;
      bh2.push(4);
      bh.merge(bh2);
    
      for (auto it = bh.ordered_begin(); it != bh.ordered_end(); ++it)
        std::cout << *it << '
    ';
      std::cout << std::boolalpha << bh2.empty() << std::endl;
      return 0;
    }

    输出为:

    4

    3

    2

    1

    true

    boost::heap::binomial_heap in addition to allowing you to iterate over elements in priority order, it also lets you merge priority queues. Elements from one queue can be added to another queue. As above, calls merge() on the queue bh. The queue bh2 is passed as a parameter. The call to merge() moves the number 4 from bh2 to bh. After the call, bh contains four numbers, and bh2 is empty. The for loop calls ordered_begin() and ordered_end() on bh. ordered_begin() returns an iterator that iterates from high priority elements to low priority elements.

    4. update

    #include <boost/heap/binomial_heap.hpp>
    #include <iostream>
    
    using namespace boost::heap;
    
    int main()
    {
      binomial_heap<int> bh;
      auto handle = bh.push(2);
      bh.push(3);
      bh.push(1);
    
      bh.update(handle, 4);
    
      std::cout << bh.top() << std::endl;
      return 0;
    }

    As above saves a handle returned by push(), making it possible to access the number 2 stored in bh.

    update() is a member function of boost::heap::binomial_heap that can be called to change an element. Afterwards, the element with the highest priority, now 4, is fetched with top().

  • 相关阅读:
    在ASP.Net和IIS中删除不必要的HTTP响应头
    java合并多个word 2007 文档 基于docx4j
    [转]怎样与 CORS 和 cookie 打交道
    css 设置div半透明 悬浮在页面底部 不随滚动条滚动
    [转]理解Vuex的辅助函数mapState, mapActions, mapMutations用法
    [转]Vue中用props给data赋初始值遇到的问题解决
    [转]import xxx from 和 import {xxx} from的区别
    [转]详解vue父组件传递props异步数据到子组件的问题
    [转]js判断数据类型的四种方法
    [转]iview的render函数用法
  • 原文地址:https://www.cnblogs.com/sssblog/p/11025031.html
Copyright © 2020-2023  润新知