C++ Boost Bimap示例详细讲解

 更新时间:2022年11月8日 11:17  点击:250 作者:无水先生

一、提要

库 Boost.Bimap 基于 Boost.MultiIndex 并提供了一个无需先定义即可立即使用的容器。该容器类似于 std::map,但支持从任一侧查找值。 Boost.Bimap 允许您根据访问地图的方式创建任意一侧都可以作为关键点的地图。当您访问左侧作为键时,右侧是值,反之亦然。

二、示例

Example13.1.Usingboost::bimap

#include <boost/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<std::string, int> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  std::cout << animals.left.count("cat") << '\n';
  std::cout << animals.right.count(8) << '\n';
}

boost::bimap 定义在 boost/bimap.hpp 中,提供了两个成员变量 left 和 right ,可用于访问 boost::bimap 统一的两个 std::map 类型的容器。在示例 13.1 中,left 使用 std::string 类型的键来访问容器,而 right 使用 int 类型的键。

除了支持使用左侧或右侧容器访问单个记录外,boost::bimap 还允许您将记录视为关系(参见示例 13.2)。

示例 13.2。访问关系

#include <boost/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<std::string, int> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  for (auto it = animals.begin(); it != animals.end(); ++it)
    std::cout << it->left << " has " << it->right << " legs\n";
}

不必使用左或右访问记录。通过迭代记录,单个记录的左右部分可通过迭代器获得。

虽然 std::map 附带一个名为 std::multimap 的容器,它可以使用相同的键存储多条记录,但 boost::bimap 没有这样的等价物。但是,这并不意味着在 boost::bimap 类型的容器中存储具有相同键的多条记录是不可能的。严格来说,两个必需的模板参数指定左和右的容器类型,而不是要存储的元素的类型。如果未指定容器类型,则默认使用容器类型 boost::bimaps::set_of。此容器与 std::map 一样,仅接受具有唯一键的记录。

示例 13.3。显式使用 boost::bimaps::set_of

#include <boost/bimap.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<boost::bimaps::set_of<std::string>,
    boost::bimaps::set_of<int>> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  std::cout << animals.left.count("spider") << '\n';
  std::cout << animals.right.count(8) << '\n';
}

示例 13.3 指定了 boost::bimaps::set_of。

除了 boost::bimaps::set_of 之外的其他容器类型可用于自定义 boost::bimap。

示例 13.4。使用 boost::bimaps::multiset_of 允许重复

#include <boost/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<boost::bimaps::set_of<std::string>,
    boost::bimaps::multiset_of<int>> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"dog", 4});
  std::cout << animals.left.count("dog") << '\n';
  std::cout << animals.right.count(4) << '\n';
}

Example13.4

示例 13.4 使用容器类型 boost::bimaps::multiset_of,它在 boost/bimap/multiset_of.hpp 中定义。它的工作方式类似于 boost::bimaps::set_of,除了键不需要是唯一的。示例 13.4 将在搜索有四条腿的动物时成功显示 2。

因为 boost::bimaps::set_of 默认用于 boost::bimap 类型的容器,所以不需要显式包含头文件 boost/bimap/set_of.hpp。但是,当使用其他容器类型时,必须包含相应的头文件。

除了上面显示的类之外,Boost.Bimap 还提供以下类:boost::bimaps::unordered_set_of、boost::bimaps::unordered_multiset_of、boost::bimaps::list_of、boost::bimaps::vector_of 和 boost: :bimaps::unconstrained_set_of。除了 boost::bimaps::unconstrained_set_of,所有其他容器类型都像标准库中的对应容器一样运行。

示例 13.5。使用 boost::bimaps::unconstrained_set_of 禁用一侧

#include <boost/bimap.hpp>
#include <boost/bimap/unconstrained_set_of.hpp>
#include <boost/bimap/support/lambda.hpp>
#include <string>
#include <iostream>
int main()
{
  typedef boost::bimap<std::string,
    boost::bimaps::unconstrained_set_of<int>> bimap;
  bimap animals;
  animals.insert({"cat", 4});
  animals.insert({"shark", 0});
  animals.insert({"spider", 8});
  auto it = animals.left.find("cat");
  animals.left.modify_key(it, boost::bimaps::_key = "dog");
  std::cout << it->first << '\n';
}

boost::bimaps::unconstrained_set_of 可用于禁用 boost::bimap 的一侧。在示例 13.5 中,boost::bimap 的行为类似于 std::map。您无法访问通过腿搜索动物的权利。

示例 13.5 说明了为什么首选 boost::bimap 而不是 std::map 的另一个原因。由于 Boost.Bimap 基于 Boost.MultiIndex,因此 Boost.MultiIndex 的成员函数可用。示例 13.5 使用 modify_key() 修改密钥——这是 std::map 无法实现的。

注意密钥是如何修改的。使用 boost::bimaps::_key 为当前键分配一个新值,这是一个在 boost/bimap/support/lambda.hpp 中定义的占位符。

boost/bimap/support/lambda.hpp 还定义了 boost::bimaps::_data。调用成员函数 modify_data() 时,boost::bimaps::_data 可用于修改 boost::bimap 类型容器中的值。

练习

使用 Boost.Bimap 实现类animals_container:

#include <boost/optional.hpp>
#include <string>
#include <vector>
#include <iostream>
struct animal
{
    std::string name;
    int legs;
    animal(std::string n, int l) : name(n), legs(l) {}
};
class animals_container
{
public:
    void add(animal a)
    {
        // TODO: Implement this member function.
    }
    boost::optional<animal> find_by_name(const std::string &name) const
    {
        // TODO: Implement this member function.
        return {};
    }
    std::vector<animal> find_by_legs(int from, int to) const
    {
        // TODO: Implement this member function.
        return {};
    }
};
int main()
{
    animals_container animals;
    animals.add({ "cat", 4 });
    animals.add({ "ant", 6 });
    animals.add({ "spider", 8 });
    animals.add({ "shark", 0 });
    auto shark = animals.find_by_name("shark");
    if (shark)
        std::cout << "shark has " << shark->legs << " legs\n";
    auto animals_with_4_to_6_legs = animals.find_by_legs(4, 7);
    for (auto animal : animals_with_4_to_6_legs)
        std::cout << animal.name << " has " << animal.legs << " legs\n";
}

到此这篇关于C++ Boost Bimap示例详细讲解的文章就介绍到这了,更多相关C++ Boost Bimap内容请搜索猪先飞以前的文章或继续浏览下面的相关文章希望大家以后多多支持猪先飞!

原文出处:https://yamagota.blog.csdn.net/article/details/127406011

[!--infotagslink--]

相关文章

  • C++ STL标准库std::vector的使用详解

    vector是表示可以改变大小的数组的序列容器,本文主要介绍了C++STL标准库std::vector的使用详解,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2022-03-06
  • C++中取余运算的实现

    这篇文章主要介绍了C++中取余运算的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++调用C#的DLL程序实现方法

    本文通过例子,讲述了C++调用C#的DLL程序的方法,作出了以下总结,下面就让我们一起来学习吧。...2020-06-25
  • C++中四种加密算法之AES源代码

    本篇文章主要介绍了C++中四种加密算法之AES源代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2020-04-25
  • C++ 整数拆分方法详解

    整数拆分,指把一个整数分解成若干个整数的和。本文重点给大家介绍C++ 整数拆分方法详解,非常不错,感兴趣的朋友一起学习吧...2020-04-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • C++万能库头文件在vs中的安装步骤(图文)

    这篇文章主要介绍了C++万能库头文件在vs中的安装步骤(图文),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-23
  • 详解C++ bitset用法

    这篇文章主要介绍了C++ bitset用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 浅谈C++中的string 类型占几个字节

    本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节...2020-04-25
  • C++ Eigen库计算矩阵特征值及特征向量

    这篇文章主要为大家详细介绍了C++ Eigen库计算矩阵特征值及特征向量,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • C++ pair的用法实例详解

    这篇文章主要介绍了C++ pair的用法实例详解的相关资料,需要的朋友可以参考下...2020-04-25
  • VSCode C++多文件编译的简单使用方法

    这篇文章主要介绍了VSCode C++多文件编译的简单使用方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
  • C++中的循环引用

    虽然C++11引入了智能指针的,但是开发人员在与内存的斗争问题上并没有解放,如果我门实用不当仍然有内存泄漏问题,其中智能指针的循环引用缺陷是最大的问题。下面通过实例代码给大家介绍c++中的循环引用,一起看看吧...2020-04-25
  • C++随机点名生成器实例代码(老师们的福音!)

    这篇文章主要给大家介绍了关于C++随机点名生成器的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C++如何删除map容器中指定值的元素详解

    map容器是C++ STL中的重要一员,删除map容器中value为指定元素的问题是我们经常与遇到的一个问题,下面这篇文章主要给大家介绍了关于利用C++如何删除map容器中指定值的元素的相关资料,需要的朋友可以参考借鉴,下面来一起看看吧。...2020-04-25
  • C++ 约瑟夫环问题案例详解

    这篇文章主要介绍了C++ 约瑟夫环问题案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...2021-08-15
  • C++中cin的用法详细

    这篇文章主要介绍了C++中cin的用法详细,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • 基于C++中常见编译错误的总结详解

    本篇文章是对C++中的常见编译错误进行了详细的分析介绍,需要的朋友参考下...2020-04-25
  • c++优先队列(priority_queue)用法详解

    这篇文章主要介绍了c++优先队列(priority_queue)用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25