The library Boost.Bimap is based on Boost.MultiIndex and provides a container that can be used immediately without being definded first. The container is similar to std::map, but supports looking up values from either side.

#include <boost/bimap.hpp>
#include <string>
#include <iostream> int main() {
boost::bimap<std::string, int> animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"spider", }); std::cout << animals.left.count("cat") << std::endl;
std::cout << aninals.right.count() << std::endl;
return ;
}

boost::bimap provides two member variables, left and right, which can be used to access the two containers of type std::map that are unified by boost::bimap.

left uses keys of type std::string to access the container, and right uses keys of type int.

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

It is not necessary to access records using left or right. By iterating over records, the left and right parts of an individual record are made vaailable through the iterator.

Strictly speaking, the two required template parameters specify container types for left and right, not the types of the elements to store. If no container type is specified, the container type boost::bimaps::set_of is used by default. This container , like std::map, only accepts records with unique keys.

#include <boost/bimap.hpp>
#include <string>
#include <iostream> int main() {
boost::bimap<boost::bimaps::set_of<std::string>, boost::bimaps::set_of<int>> animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"spider", }); std::cout << animals.left.count("cat") << std::endl;
std::cout << aninals.right.count() << std::endl;
return ;
}

2. allowing duplicates with boost::bimaps::multiset_of

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

boost::bimaps::multiset_of the keys don't need to be unique.

3. In addition to the classes shown above, Boost.Bimap provides the following boost::bimaps::unordered_set_of, boost::bimaps::unordered_multiset_of, boost::bimaps::list_of, boost::bimaps::vector_of, and boost::bimaps::unconstrained_set_of. Except for boost::bimaps::unconstrained_set_of, all of the other container types operate just like their counterparts from the standard library.

#include <boost/bimap.hpp>
#include <string>
#include <iostream> int main() {
boost::bimap<std::string, boost::bimaps::unconstrained_set_of<int>> animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"spider", }); auto it = animals.left.find("cat");
animals.left.modify_key(it, boost::bimaps::_key = "dog"); std::cout << it->first << std::endl;
return ;
}

输出: dog

boost bimap的更多相关文章

  1. Boost Bimap示例

    #include <string> #include <iostream> #include <boost/bimap.hpp> template< clas ...

  2. boost容器bimap简单使用

    C++标准提供了map和multi_map,把key映射到value;    但是这种映射是单向的,只能是key到value,不能反过来;    boost.bimap扩展了标准库映射型容器,提供双向 ...

  3. Boost 1.61.0 Library Documentation

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

  4. boost 使用列子

    #include <boost/lexical_cast.hpp>void test_lexical_cast(){ int number = 123; string str = &quo ...

  5. boost之数据结构和容器

    1.静态数组array,boost对静态数组进行了封装,使用和普通数组一样的初始化式进行初始化. #include <iostream> #include <boost/array. ...

  6. boost开发指南

    C++确实很复杂,神一样的0x不知道能否使C++变得纯粹和干爽? boost很复杂,感觉某些地方有过度设计和太过于就事论事的嫌疑,对实际开发工作的考虑太过于理想化.学习boost本身就是一个复杂度,有 ...

  7. Google C++ Style Guide

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

  8. 1.1. 如何使用XproerUI库

    项目类型:MFC   XproerUI结构: 3rd                    第三方库目录   cximage     dll                编译的DLL目录   pug ...

  9. 如何使用XproerUI库(WTL)-XproerUI界面库教程

    版权所有 2009-2015 荆门泽优软件有限公司 保留所有权利 产品首页:http://www.ncmem.com/apps/xproerui/index.asp 开发文档(SkinStudio): ...

随机推荐

  1. PHP读取XML文件数据获取节点值

    最近在接入渠道的时候遇到接口返回是xml数据.现在接口数据返回json数据格式比较常见. 如何获取xml里面真正数据? 对象结果集合单个值的强制转换处理.(直接代码说明) demo示例:  创建xml ...

  2. python判断字符串是否是json格式方法分享

    python判断字符串是否是json格式方法分享 在实际工作中,有时候需要对判断字符串是否为合法的json格式 解决方法使用json.loads,这样更加符合'Pythonic'写法 代码示例:   ...

  3. apicloud地图、即时通讯、人脸识别登录、以及平时踩过得坑

    apicloud技术浅谈 导语 apicloud 的学习也有一段时间了,这是我个人的一些经验,和踩过的坑,希望对大家能有一些帮助. apicloud的知识准备 apicloud 是一个用原生的思想搭建 ...

  4. Codeforces Round #285 (Div. 2)C. Misha and Forest(拓扑排序)

    传送门 Description Let's define a forest as a non-directed acyclic graph (also without loops and parall ...

  5. php用什么软件编程

    准备好好学习学习PHP了吗?那么你首先应该考虑用什么开发工具(IDE).市面上有很多这类工具,收费的有,免费的也有,选择起来并不轻松. 如果你说PHP编程用基础的文本编辑软件就可以了,比如用记事本.是 ...

  6. 01 - Jmeter4.x环境安装以及简单使用

    Jmeter 介绍 Apache JMeter应用程序是开源软件,旨在为负载测试功能行为和测量性能的100%纯Java应用程序.它最初是为测试Web应用程序而设计的,但后来扩展到其他测试功能 常用压力 ...

  7. Java双链表

    一.概述 二.英雄类 class HeroNode { //值域 public int id; public String name; public String nickName; //指针域 pu ...

  8. 15、numpy——排序、条件刷选函数

    NumPy 提供了多种排序的方法. 这些排序函数实现不同的排序算法,每个排序算法的特征在于执行速度,最坏情况性能,所需的工作空间和算法的稳定性. 下表显示了三种排序算法的比较. 种类 速度 最坏情况 ...

  9. NGUI的CheckBox的使用(toggle script)

    一,我们先添加一个sprite,选择sprite,右键选择attach,添加box collider, 然后右键选择attach,添加toggle script,得到如下图结果 1,但是如果你没有给U ...

  10. NGUI的下拉框的使用(popup list script)

    一,我们先添加一个sprite,选择sprite,右键选择attach,添加box collider, 然后右键选择attach,添加popup list script ,我们给popup list ...