C++ Boost Container库有哪些功能

2023-03-13

本文小编为大家详细介绍“C++ Boost Container库有哪些功能”,内容详细,步骤清晰,细节处理妥当,希望这篇“C++ Boost Container库有哪些功能”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

一、关于Boost.Container

Boost.Container

Boost.Container 是一个 Boost 库,提供与标准库相同的容器。 Boost.Container 专注于额外的灵活性。例如,这个库中的所有容器都可以在共享内存中与 Boost.Interprocess 一起使用——这对于标准库中的容器并不总是可行的。

Boost.Container 提供了额外的优势:

  • 容器的接口类似于 C++11 标准库中容器的接口。例如,它们提供诸如 emplace_back() 之类的成员函数,您可以在 C++98 程序中使用它,即使它直到 C++11 才被添加到标准库中。

  • 借助 boost::container::slist 或 boost::container::stable_vector,Boost.Container 提供了标准库不提供的容器。

  • 该实现与平台无关。容器在任何地方的行为都相同。您无需担心标准库实现之间可能存在的差异。

  • Boost.Container 中的容器支持不完整的类型,可用于定义递归容器。

二、Boost.Container示例

示例 20.1 说明了不完整的类型。

注意

本章中的示例无法使用 Visual C++ 2013 和 Boost 1.55.0 编译。此错误在工单 9332 中进行了描述。它已在 Boost 1.56.0 中修复。

示例 20.1。带有 Boost.Container 的递归容器

#include <boost/container/vector.hpp>
using namespace boost::container;
struct animal
{
  vector<animal> children;
};
int main()
{
  animal parent, child1, child2;
  parent.children.push_back(child1);
  parent.children.push_back(child2);
}

类动物有一个类型为 boost::container::vector<animal> 的成员变量 children。 boost::container::vector 在头文件 boost/container/vector.hpp 中定义。因此,成员变量children 的类型基于定义变量children 的类animal。在这一点上,还没有完全定义动物。虽然该标准不要求标准库中的容器支持不完整类型,但 Boost.Container 明确支持递归容器。标准库定义的容器是否可以递归使用取决于实现。

示例 20.2。使用 boost::container::stable_vector

#include <boost/container/stable_vector.hpp>
#include <iostream>
using namespace boost::container;
int main()
{
  stable_vector<int> v(2, 1);
  int &i = v[1];
  v.erase(v.begin());
  std::cout << i << '\n';
}

除了标准库中众所周知的容器之外,Boost.Container 还提供容器。示例 20.2 引入了容器 boost::container::stable_vector,其行为类似于 std::vector,除了如果 boost::container::stable_vector 更改,所有迭代器和对现有元素的引用仍然有效。这是可能的,因为元素没有连续存储在 boost::container::stable_vector 中。即使元素没有彼此相邻存储在内存中,仍然可以使用索引访问元素。

Boost.Container 保证示例 20.2 中的引用 i 在向量中的第一个元素被擦除时仍然有效。该示例显示 1。

请注意,boost::container::stable_vector 和该库中的其他容器都不支持 C++11 初始化列表。在示例 20.2 中,v 被初始化为两个元素都设置为 1。

boost::container::stable_vector 在 boost/container/stable_vector.hpp 中定义。

Boost.Container 提供的其他容器是 boost::container::flat_set、boost::container::flat_map、boost::container::slist 和 boost::container::static_vector:

  • boost::container::flat_set 和 boost::container::flat_map 类似于 std::set 和 std::map。然而,它们被实现为排序向量,而不是树。这允许更快的查找和迭代,但插入和删除元素的成本更高。

  • 这两个容器在头文件 boost/container/flat_set.hpp 和 boost/container/flat_map.hpp 中定义。

  • boost::container::slist 是一个单链表。它类似于使用 C++11 添加到标准库的 std::forward_list。

  • boost::container::slist 提供了一个成员函数 size(),它在 std::forward_list 中没有。

  • boost::container::slist 在 boost/container/slist.hpp 中定义。

  • boost::container::static_vector 将 std::array 等元素直接存储在容器中。与 std::array 一样,容器具有恒定的容量,尽管容量并没有说明元素的数量。成员函数 push_back()、pop_back()、insert() 和erase() 可用于插入或删除元素。在这方面, boost::container::static_vector 类似于 std::vector。成员函数 size() 返回容器中当前存储元素的数量。

  • 容量是恒定的,但可以使用 resize() 更改。 push_back() 不会改变容量。仅当容量大于当前存储的元素数量时,您才可以使用 push_back() 添加元素。否则,push_back() 会抛出 std::bad_alloc 类型的异常。

boost::container::static_vector 在 boost/container/static_vector.hpp 中定义。

读到这里,这篇“C++ Boost Container库有哪些功能”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注本站行业资讯频道。

《C++ Boost Container库有哪些功能.doc》

下载本文的Word格式文档,以方便收藏与打印。