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): ...
随机推荐
- xiugai完了
<!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Conten ...
- 架构-SOA:SOA(面向服务的架构)
ylbtech-架构-SOA:SOA(面向服务的架构) 面向服务的架构(SOA)是一个组件模型,它将应用程序的不同功能单元(称为服务)进行拆分,并通过这些服务之间定义良好的接口和契约联系起来.接口是采 ...
- Java常用工具——java多线程
一.线程的创建 方式一:继承Thread类,重写run()方法 package com.imooc.thread1; class MyThread extends Thread{ public MyT ...
- 聊聊redis的监控工具
序 本文主要研究一下redis的监控工具 redis-stat redis-stat是一个比较有名的redis指标可视化的监控工具,采用ruby开发,基于redis的info命令来统计,不影响redi ...
- 深入探讨vue响应式原理
现在是时候深入一下了!Vue 最独特的特性之一,是其非侵入性的响应式系统.数据模型仅仅是普通的 JavaScript 对象.而当你修改它们时,视图会进行更新.这使得状态管理非常简单直接,不过理解其工作 ...
- python的tesseract库几个重要的命令
在调用tesseract时,最重要的三个参数是 -l, -oem 和 -psm -l 参数控制识别文本的语言.可以通过命令 tesseract --list-langs 查看已经安装的字库. 支持中 ...
- 网页导出excel
package site.action.ecom.backend.wechat.exportExcel; import java.lang.annotation.Documented;import j ...
- ES6 find 和 filter 的区别
ES6 find 和 filter 的区别 : 遇到个功能是要分类就想说在前端过滤,不要从查数据库的时候过滤了.然后就想说除了filter还有啥好用的 发现有个find,测试一番之后发现 const ...
- java中高级开发知识准备要点
转载来源:https://www.cnblogs.com/JavaArchitect/p/10011253.html 在上周,我密集面试了若干位Java后端的候选人,工作经验在3到5年间.我的标准其实 ...
- [CodeForces 52C]Circular RMQ
题目传送门 评分:省选/NOI-,难度:普及+/提高 这题真的和RMQ没有半点关系,只需要一个裸的线段树,连pushdown都不需要,只需要两种操作:区间修改和区间求最小值,在回溯时加上标记即可,唯一 ...