boost之bimaps

本文介绍了Boost库中的Boost.Bimap容器,其支持双向查找,具有键值可配置、丰富的接口、性能优化和类型安全特性。通过实例展示了如何在实际项目中使用和操作这个容器。
摘要由CSDN通过智能技术生成

Boost.Bimap 是 Boost 库中的一个容器,它支持双向映射,即允许通过键查找值,也允许通过值查找键。 Boost.Bimap 的主要功能介绍如下:

  • 双向查找:与 STL 中的 map 和 multimap 不同,这两者只能进行单向映射,即通过键(key)来查找值(value)。Boost.Bimap 提供了双向查找的功能,这意味着你可以通过键找到值,也可以通过值找到对应的键。
  • 可配置性: 键、值和键值组合是否可重复都可以配置,是否有序也可以配置。
  • 实际应用:在实际的项目开发中,当需要双向查找时,使用 Boost.Bimap 会非常方便。例如,如果你有一个场景,既需要知道某个员工的 ID 对应的姓名,也需要知道某个姓名对应的 ID,那么 Boost.Bimap 就是一个非常好的选择。
  • 接口丰富:除了基本的查找功能,Boost.Bimap 还提供了一系列的接口和方法,用于操作和访问容器中的元素,如插入、删除、遍历等。
  • 性能优化:Boost.Bimap 在内部进行了优化,以确保双向查找的性能。虽然在使用时感觉像是两个独立的容器,但实际上 Boost.Bimap 在内部只维护了一个数据结构,这有助于提高查找效率。
  • 类型安全:Boost.Bimap 支持强类型,这意味着在编译时就会检查键和值的类型是否匹配,从而避免潜在的错误。
  • 易于使用:尽管 Boost.Bimap 提供了强大的功能,但它的使用方式相对简单直观。通过阅读官方文档和相关教程,开发者可以快速上手并应用到项目中。

示例代码:

#include <string>
#include <iostream>
using namespace std;

#include <boost/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/bimap/list_of.hpp>
#include <boost/bimap/unordered_set_of.hpp>
#include <boost/bimap/vector_of.hpp>
#include <boost/bimap/support/lambda.hpp>
using map_type = boost::bimaps::bimap<
    boost::bimaps::multiset_of<string>, 
    boost::bimaps::multiset_of<int>,
    //boost::bimaps::vector_of_relation>;
    //boost::bimaps::list_of_relation>;
    //boost::bimaps::left_based>;
    boost::bimaps::set_of_relation<>>;cl
using value_type = map_type::value_type;

int main()
{
    map_type m;
    
    //list_of_relation, vector_of_relation
    // m.push_back(value_type("aa", 4));
    // m.push_back(value_type("bb", 5));
    // m.push_back(value_type("cc", 3));
    // m.push_back(value_type("dd", 1));
    // m.push_back(value_type("ee", 2));
    // m.push_back(value_type("ff", 6));
    // m.push_back(value_type("zz", 3));
    // m.push_back(value_type("aa", 6));
    // m.push_back(value_type("aa", 4));

    //set_of_relation, multiset_of_relation, unordered_set_of_relation, unordered_multiset_of_relation
    m.insert(value_type("aa", 4));
    m.insert(value_type("bb", 5));
    m.insert(value_type("cc", 3));
    m.insert(value_type("dd", 1));
    m.insert(value_type("ee", 2));
    m.insert(value_type("ff", 6));
    m.insert(value_type("zz", 3));
    m.insert(value_type("aa", 6));
    m.insert(value_type("aa", 4));
    for(auto& item : m)
        cout << item.left << ":" << item.right << "     " << item.right << ":" << item.left << endl;
    cout << "----------------" << endl;

    for(auto& item : m.right)
        cout << item.first << ":" << item.second << endl;
    cout << "----------------" << endl;
    cout << (m.left.find("cc") != m.left.end()) << endl;
    m.left.erase("cc");                      //删除
    cout << m.left.count("cc") << endl;      
    cout << m.size() << endl;


    auto it = m.left.find("aa");
    m.left.replace_key(it, "aa1");   //修改键
    it = m.left.find("aa");
    m.left.modify_key(it, boost::bimaps::_key="aa2"); //另一种修改键的方式

    m.left.modify_data(it, boost::bimaps::_data=123);   //修改值
    it = m.left.find("aa1");
    m.left.replace_data(it, 1234);                      //修改值
    cout << "----------------" << endl;
    for(auto& item : m)
        cout << item.left << ":" << item.right << "     " << item.right << ":" << item.left << endl;

    return 0;
}

输出:

  • 23
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值