boost bimap
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的更多相关文章
- Boost Bimap示例
#include <string> #include <iostream> #include <boost/bimap.hpp> template< clas ...
- boost容器bimap简单使用
C++标准提供了map和multi_map,把key映射到value; 但是这种映射是单向的,只能是key到value,不能反过来; boost.bimap扩展了标准库映射型容器,提供双向 ...
- Boost 1.61.0 Library Documentation
http://www.boost.org/doc/libs/1_61_0/ Boost 1.61.0 Library Documentation Accumulators Framework for ...
- boost 使用列子
#include <boost/lexical_cast.hpp>void test_lexical_cast(){ int number = 123; string str = &quo ...
- boost之数据结构和容器
1.静态数组array,boost对静态数组进行了封装,使用和普通数组一样的初始化式进行初始化. #include <iostream> #include <boost/array. ...
- boost开发指南
C++确实很复杂,神一样的0x不知道能否使C++变得纯粹和干爽? boost很复杂,感觉某些地方有过度设计和太过于就事论事的嫌疑,对实际开发工作的考虑太过于理想化.学习boost本身就是一个复杂度,有 ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- 1.1. 如何使用XproerUI库
项目类型:MFC XproerUI结构: 3rd 第三方库目录 cximage dll 编译的DLL目录 pug ...
- 如何使用XproerUI库(WTL)-XproerUI界面库教程
版权所有 2009-2015 荆门泽优软件有限公司 保留所有权利 产品首页:http://www.ncmem.com/apps/xproerui/index.asp 开发文档(SkinStudio): ...
随机推荐
- 面向对象编程思想(OOP)(转发)
本文我将从面向对象编程思想是如何解决软件开发中各种疑难问题的角度,来讲述我们面向对象编程思想的理解,梳理面向对象四大基本特性.七大设计原则和23种设计模式之间的关系. 软件开发中疑难问题: 软件复杂庞 ...
- 用redis实现队列实例
queue input #coding = utf-8__autor__ = 'litao'import redisimport timepool = redis.ConnectionPool(hos ...
- gitlab+jenkins自动化打包APK
前置条件: 环境搭建,jenkins需要的插件看这里: gitlab+jenkins自动化打包IOS 配置思路: step1: 搭建sdk,gradle运行环境,参照: CentOS7下安装安装and ...
- day37—javascript对表格table的操作应用(二)
转行学开发,代码100天——2018-04-22 昨天学习了JavaScript对table的基本操作,包括表格的创建,表格元素的获取,隔行换色及鼠标动作等.今天主要学习table的搜索查询及排序操作 ...
- 【ABAP系列】SAP SAP中关于编码的解释
公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP SAP中关于编码的解释 ...
- java--反射原理及操作
1.反射原理 反射具体操作 15.反射的原理(********理解********) * 应用在一些通用性比较高的代码 中 * 后面学到的框架,大多数都是使用反射来实现的 * 在框架开发中,都是基于配 ...
- 求bit中1的个数有几种做法
原文 求bit中1的个数有几种做法: - x & (x - 1) - Hamming weight的经典求法,基于树状累加:http://en.wikipedia.org/wiki/Hammi ...
- 11 (H5*) js第1天 基本数据类型、变量
目录 1: js的介绍 2:写js代码注意的地方 3:变量 4:变量的命名和作用 5:变量的类型 6:Number类型 7:string类型 8:类型转换 9:操作符号 复习 <script& ...
- 自定义SAP搜索帮助记录-代码实现
一般来说,标准的字段都可以用SE11来创建搜索帮助,但是有时候这里的满足不了需求或者,相同的数据元素需要用不同的搜索帮助类型,就需要用别的方式实现 1.用函数:F4IF_INT_TABLE_VALUE ...
- spring容器的启动过程
spring的启动过程: 首先,对于一个web应用,其部署在web容器中,web容器提供其一个全局的上下文环境,这个上下文就是ServletContext,其为后面的spring IoC容器提供宿主环 ...