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();
pq.push();
pq.push(); for (int i : pq) {
std::cout << i << std::endl;
} priority_queue<int> pq2;
pq2.push();
std::cout << std::boolalpha << (pq > pq2) << std::endl;
return ;
}

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();
bh.push();
bh.push(); binomial_heap<int> bh2;
bh2.push();
bh.merge(bh2); for (auto it = bh.ordered_begin(); it != bh.ordered_end(); ++it)
std::cout << *it << '\n';
std::cout << std::boolalpha << bh2.empty() << std::endl;
return ;
}

输出为:

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();
bh.push();
bh.push(); bh.update(handle, ); std::cout << bh.top() << std::endl;
return ;
}

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().

boost heap的更多相关文章

  1. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  2. zz A list of open source C++ libraries

    A list of open source C++ libraries < cpp‎ | links http://en.cppreference.com/w/cpp/links/libs Th ...

  3. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

  4. c++开发规范

    目录 1. 头文件 1.1. Self-contained 头文件 1.2. #define 保护 1.3. 前置声明 1.4. 内联函数 1.5. #include 的路径及顺序 2. 作用域 2. ...

  5. 你说你会C++? —— 智能指针

    智能指针的设计初衷是:      C++中没有提供自己主动回收内存的机制,每次new对象之后都须要手动delete.稍不注意就memory leak. 智能指针能够解决上面遇到的问题. C++中常见的 ...

  6. Google开源项目风格指南

    Google开源项目风格指南 来源 https://github.com/zh-google-styleguide/zh-google-styleguide Google 开源项目风格指南 (中文版) ...

  7. [Guide]Google C++ Style Guide

    0.0 扉页 项目主页 Google Style Guide Google 开源项目风格指南 -中文版 0.1 译者前言 Google 经常会发布一些开源项目, 意味着会接受来自其他代码贡献者的代码. ...

  8. Boost 1.61.0 Library Documentation

    http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...

  9. boost库的安装,使用,介绍,库分类

    1)首先去官网下载boost源码安装包:http://www.boost.org/ 选择下载对应的boost源码包.本次下载使用的是 boost_1_60_0.tar.gz (2)解压文件:tar - ...

随机推荐

  1. Uva 12563 Jin Ge Jin Qu hao(01背包)

    题意: 假定你在唱KTV,还剩下t秒时间.你决定接下来唱你最喜爱的n首歌(不包含劲歌金曲)中的一些歌曲.在时间结束之前再唱一个劲歌金曲.使得唱的歌的总曲目尽量多以及时间总长度. 输入保证所有n+1曲子 ...

  2. 代理池抓取基础版-(python协程)--抓取网站(西刺-后期会持续更新)

    # coding = utf- __autor__ = 'litao' import urllib.request import urllib.request import urllib.error ...

  3. algorithm_action

    求矩阵Amk.Bkn的乘积 for(i=1;i<=m;i++) for(j=1;j<=n;j++) cij = 0 for(p=1;p<=k;p++) cij += aip*bpj

  4. 2019/10/27 TZOJ

    1001 Gaussian Prime http://www.tzcoder.cn/acmhome/problemdetail.do?&method=showdetail&id=379 ...

  5. Buy Tickets 【POJ - 2828】【线段树】

    题目链接 有N次操作,每次都是将第i个数放置在第pos个数的后面,并且这个数的值是val. 这个线段树的思维确实很好,我们可以发现,后面放进去的数,一定是强制位置的,而前面放的数,会随着后面的数进入而 ...

  6. Autoencoder基本操作及其Tensorflow实现

    最近几个月一直在和几个小伙伴做Deep Learning相关的事情.除了像tensorflow,gpu这些框架或工具之外,最大的收获是思路上的,Neural Network相当富余变化,发挥所想.根据 ...

  7. linux + eclipse + cdt 报错undefined reference......好麻烦的,这位大牛给出的方法可行,特此MARK!!!!

    http://bbs.csdn.net/topics/390239632 kerosun kerosun 等级: 结帖率:96.92% 楼主 发表于: 2012-10-11 12:00:51   比如 ...

  8. Windows下Nginx的启动、停止、重启等命令

    Windows下Nginx的启动.停止等命令 在Windows下使用Nginx,我们需要掌握一些基本的操作命令,比如:启动.停止Nginx服务,重新载入Nginx等,下面我就进行一些简单的介绍. 假设 ...

  9. Mac下安装nodejs,然后安装Vue-devtools工具

    一.安装nodejs 1.这一步简单,只要上官网下载下来,直接按照提示安装就可以,mac版本的安装方法很简单. 下载nodejs的官方网址是:  nodejs.org    ,浏览器输入就可以跳转到了 ...

  10. 如何查找django安装路径

    需要找到django的安装路径,官方说的那个方法不好用,国内搜索都是都不到的,后来谷歌搜到了很简单 import django django 这样就可以找django的安装路径了,真心不懂为什么国内都 ...