boost heap

2022-11-16 208481 编程学习

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 << '\n';
  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().

Linux下怎么安装boost 1.69库

这篇文章主要介绍了Linux下怎么安装boost1.69库的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇Linux下怎么安装boost1.69库文章都会有所收获,下面我们一起来看看吧。Boost库是为C++语言标准库提供的一些扩展功能,包括算法库、模板元编程、数据结构库、图像处理库、迭代器库,输入输出库等,可大大提高软件的开发效率。测试平台Linux版本:Ubuntu...

NDK 编译 boost库

从GITHUB上拉别人一个boost工程https://github.com/moritz-wundke/Boost-for-Android编译需要MinGW(MinGNUforWindow)下载了MinGW下载器 http://www.mingw.org/wiki/MSYS然后将bin路径加入到path环境变量中C:\msys\1.0\binMingw运行ls提示有报错couldn't...

C++中Boost的智能指针scoped_ptr怎么使用

本篇内容主要讲解“C++中Boost的智能指针scoped_ptr怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“C++中Boost的智能指针scoped_ptr怎么使用”吧!boost::scoped_ptr和std::auto_ptr非常类似,是一个简单的智能指针,它能够保证在离开作用域后对象被自动释放。下列代码演示了该指针的基本应用:#in...

boost thread 类应用以及资源耗尽异常

1测试例子#include<iostream>#include<string>#include<boost/thread/thread.hpp>voidThreadFunc(){std::cout<<"Welcometothreadfunction"<<std::endl;}intmain(intargc,char*argv[]){boo...

(C++)(boost)(thread)build helloworld.cpp

编译环境:Ubuntu+boostlibs编译命令:$g++helloworld.cpp-lboost_thread-lboost_system-L/usr/lib

3 C++ Boost 字符,文本

3C++Boost字符,文本目录: 字符与数值转换 Boost format函数 简单实用 Boost format 输出人员信息 小案例 Boost format 数字处理 Boost format 高级特性 Boost String 处理,大小写转换 Boost String...

如何在VS2013中安装与使用Boost库

今天就跟大家聊聊有关如何在VS2013中安装与使用Boost库,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。Boost安装2.1Boost官网下载Boost最新版Version1.55.02.2将下载压缩包解压到本地解压后可看到目录下有个bootstrap.bat文件。2.3打开cmd命令窗体,执行bootstra.bat文件运行下面命令,...

visual studio 2015下boost库配置的示例分析

这篇文章主要介绍visualstudio2015下boost库配置的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!首先,我们需要下载一个Boost库。这个直接去他的官网下就可以了:boost下载地址下载好后解压到一个目录里。比如我解压到D盘根目录 然后我们打开【开始菜单】->【找到你对应版本的vs命令窗口,比如我是vs2015的】P.s:一定要对应自...