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): ...
随机推荐
- IBatis.Net 下使用SqlBulkCopy 大批量导入数据 问题解决
SQLBulkCopy是继承SQLClient空间下的一个特殊类,它可以帮助我们以映射的方式把DataTable和DataReader数据大批量导入到数据库对应表中 public void Inert ...
- jsc2019_qualD Classified
题目大意 给你一个有n个点的完全图 求一种方案是的给边染色后任何一点不能沿一种颜色的边走奇数条边回到这个点 要求颜色数最少 分析 考场上输出格式打错见祖宗... 我们每次找一个最大二分图将其染一个新颜 ...
- android7.0对于SharedPreferences设置模式的限制
错误信息: 03-28 10:16:12.701 830 932 E AndroidRuntime: FATAL EXCEPTION: Thread-903-28 10:16:12.701 ...
- WPF数据模板中绑定事件不触发问题
今天比较闲,做一个练手的项目,结果在xaml中写了一个用户的数据模板后,在其中的某个Canvas上绑定了一个鼠标左击的事件,结果调试的时候,无论怎么点击都不跳到断点那里,百思不得其解. 之后尝试不绑定 ...
- Redux生态系统
生态系统 Redux 是一个体小精悍的库,但它相关的内容和 API 都是精挑细选的,足以衍生出丰富的工具集和可扩展的生态系统. 如果需要关于 Redux 所有内容的列表,推荐移步至 Awesome R ...
- Altium Designer chapter6总结
绘制PCB中需要注意的如下: (1)网络表的载入:网络表是原理图与PCB之间的桥梁,而AD实现了真正的双向同步设计.在装入网表之前需要先添加相应的封装库. (2)元件的布局:一般采用手工布局:按照模块 ...
- datepart()函数的使用
/* datepart()函数的使用 * datepart()函数可以方便的取到时期中的各个部分 *如日期:2006-07--02 18:15:36 ...
- 《剑指offer》面试题4 替换空格 Java版
(给一个足够长的字符数组,其中有一段字符,将' '(空格)替换成'%' '2' '0'三个字符,原字符段由'\0'结尾) 书中方法:这道题如果从头到尾扫描数组并替换,会涉及到数组的移动.如果不移动元素 ...
- 重温位运算、原码、反码、补码、以及>>和<<<区别
一个例子说明原码,反码,补码: 下面进行5和-5的原码,反码,补码表示: 5的原码:0000 0101 5的反码:0000 0101 5的补码:0000 0101 -5的原码:1000 0101 -5 ...
- LeetCode 852. Peak Index in a Mountain Array(C++)
Let's call an array A a mountain if the following properties hold: A.length >= 3 There exists som ...