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): ...
随机推荐
- java 将数据库中的每一条数据取出放入数组或者List中
1.如何将数据库中数据按照行(即一整条数据)取出来,存入到数组当中? public static String str = null; // 将StringBuffer转化成字符串 public st ...
- change可以重命名列名,也可能修改列的类型和约束,而modify只能修改列的数据类型。
change 可以重命名列名,也可能修改列的数据类型,而modify只能修改列的数据类型. change 比modify功能多,如果要用change修改字段类型和约束,要加上新字段名,新字段名可以和原 ...
- day37—javascript对表格table的操作应用(二)
转行学开发,代码100天——2018-04-22 昨天学习了JavaScript对table的基本操作,包括表格的创建,表格元素的获取,隔行换色及鼠标动作等.今天主要学习table的搜索查询及排序操作 ...
- day22—一个AngularJS框架应用toDoList
转行学开发,代码100天——2018-04-07 今天用AngularJS照着课程写了一个案例,即toDoList,记事清单效果. 主要实现以下效果: 1.通过文本框添加内容,同时添加事件列表.主要用 ...
- php基础/类型
1.php的格式: <?php ?> 内嵌格式: <? ?> (php可以写在html文件里面) 2.php的输出:echo (每段的结束必须加;) 3.定义变量: 不需要管他 ...
- DELPHI 全局变量和局部变量的区别
全局变量: 如果我们在应用程序一个单元中的interface关键字和implementation关键字之间的区域,定义一个全局变量,假如这个单元在别的地方被引用,那么这个单元的全 局变量能够在别的地方 ...
- curl 中关于 CURLINFO_HEADER_SIZE 的 BUG 定位及修复
curl 官方下载页面 CentOS7 默认安装的 curl 版本太低了,需要升级为最新版. 1. 问题描述 对接了一个接口,用来下载 PDF 文件.使用 curl 下载后,文件老是报错无法打开.接口 ...
- 【题解】4879. 【NOIP2016提高A组集训第11场11.9】少女觉
Description 在幽暗的地灵殿中,居住着一位少女,名为古明地觉.据说,从来没有人敢踏入过那座地灵殿,因为人们恐惧于觉一族拥有的能力——读心.掌控人心者,可控天下. 咳咳.人的记忆可以被描述为一 ...
- Fira Code,可以让不等号!=直接显示出来的字体
今天看B站某直播间有人写代码C#里一堆不等号直接显示,感觉很神奇,以为是插件还是什么新语法,托人问了下原来是Fira Code字体 https://github.com/tonsky/FiraCode ...
- 15.队列Queue的特点以及使用,优先级等
#生产者与消费者模式,模式解释:比如MVC设计模式 ''' 1.队列 (1)特点:先进先出 (2)python2 VS python3 python2:from Queue import queue ...