boost multi index
Boost.MultiIndex makes it possible to define containers that support an arbitrary number of interfaces. While std::vector provides an interface that supports direct access to elements with an index and std::set provides an interface that sorts elements. Boost.MultiIndex lets you definde containers that support both interfaces. Such a container could be used to access elements using an index and in a sorted fashion.
1.
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
#include <iostream> using namespace boost::multi_index; struct animal {
std::string name;
int legs;
}; typedef multi_index_container<
animal,
indexed_by<
hashed_non_unique<
member<
animal, std::string, &animal::name
>
>,
hashed_non_unique<
member<
animal, int, &animal::legs
>
>
>
> animal_multi; int main() {
animal_multi animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"spider", }); std::cout << animals.count("cat") << std::endl; const animal_multi::nth_index<>::type& legs_index = animals.get<>();
std::cout << legs_index.count() << std::endl;
return ;
}
When you use Boost.MultiIndex, the first step is to define a new container. You have to decide which interfaces your new container should support and which element properties it should access.
multi_index_container is a class template that requires at least two parameters. The first parameter is the type of elements the container should store. The second parameter is used to denote different indexes the container should provide.
The key advantage of containers based on Boost.MultiIndex is that you can access elements via different interfaces. When you define a new container, you can specify the number and type of interfaces.
The advantage of the container animal_multi over a map like std::unordered_map is that animals can be looked up by name or by number of legs. animal_multi supports two interfaces, one based on the name and one based on the number of legs. The interface determines which member variable is the key and which member variable is the value. Because data such as names and legs can be keys of the MultiIndex container, the cannot be arbitrarily changed. If the number of legs is changed after an animal hase been looked up by name, an interface using legs as a key would be unaware of the change and would not know that a new hash value needs to be calculted.
2. boost::multi_index::hashed_unique
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
#include <iostream> using namespace boost::multi_index; struct animal {
std::string name;
int legs;
}; typedef multi_index_container<
animal,
indexed_by<
hashed_non_unique<
member<
animal, std::string, &animal::name
>
>,
hashed_unique<
member<
animal, int, &animal::legs
>
>
>
> animal_multi; int main() {
animal_multi animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"dog", }); auto& legs_index = animals.get<>();
std::cout << legs_index.count() << std::endl;
return ;
}
输出:1
hashed_non_unique which calculates a hash value that does not have to be unique. In order to guarantee that no value is stored twice, use boost::multi_index::hashed_unique. If one interface does not allow you to store values multiple times, it does not matter whether another interface does allow it. The example tries to store a dog, which has the same number of legs as the already stored cat. Because this violates the requirement of having unique hash values for the second interface, the dog will not be stored in the container. Therefore, when searching for animals with four legs, the program displays 1, because only the cat was stored and counted.
3. The interfaces sequenced, ordered_noe_unique, random_access
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/sequenced_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/random_access_index.hpp>
#include <boost/multi_index/member.hpp>
#include <string>
#include <iostream> using namespace boost::multi_index; struct animal {
std::string name;
int legs;
}; typedef multi_index_container<
animal,
indexed_by<
sequenced<>,
ordered_non_unique<
member<
animal, int, &animal::legs
>
>,
random_access<>
>
> animal_multi; int main() {
animal_multi animals; animals.insert({"cat", });
animals.insert({"shark", });
animals.insert({"spider", }); auto& legs_index = animals.get<>();
auto it = legs_index.lower_bound();
auto end = legs_index.upper_bound(); for (; it != end; ++it) {
std::cout << it->name << std::endl;
} const auto& rand_index = animals.get<>();
std::cout << rand_index[].name << std::endl;
return ;
}
The interface boost::multi_index::sequenced allows you to treat a MultiIndex container like a list of type std::list. Elements are stored in the given order.
With the interface boost::multi_index::ordered_non_unique, objects are automatically sorted. This interface requires that you specify a sorting criterion when defining the container. ordered_non_unique provides special member functions to find specific ranges within the sorted values. Using lower_bound() and upper_bound(), the program searches for animals that have at lease four and no more than eight legs.
boost::multi_index::random_access allows you to treat the MultiIndex container like a vector of type std::vector. The two most prominent member functions are operator[] and at().
4. The key extractors identity and const_mem_fun
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <string>
#include <utility>
#include <iostream> using namespace boost::multi_index; class animal {
public:
animal(std::string name, int legs) : name_(std::move(name)), legs_(legs) {}
bool operator<(const animal& a) const {
return legs_ < a.legs_;
}
const std::string& name() const {
return name_;
}
private:
std::string name_;
int legs_;
}; typedef multi_index_container<
animal,
indexed_by<
ordered_unique<
identity<animal>
>,
hashed_unique<
const_mem_fun<
animal, const std::string&, &animal::name
>
>
>
> animal_multi; int main() {
animal_multi animals; animals.emplace("cat", );
animals.emplace("shark", );
animals.emplace("spider", ); std::cout << animals.begin()->name() << std::endl; const auto& name_index = animals.get<>();
std::cout << name_index.count("shark") << std::enl;
return ;
}
boost::multi_index::identity uses elements stored in the container as keys. This requires the class animal to be sortable because objects of type animal will be used as the key for the interface boost::multi_index::ordered_unique. This is achieved through the overloaded operator<
boost::multi_index::const_mem_fun and boost::multi_index::mem_fun that use the return value of a member function as a key.
boost multi index的更多相关文章
- boost multi array
Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...
- 基础:从概念理解Lucene的Index(索引)文档模型
转:http://blog.csdn.net/duck_genuine/article/details/6053430 目录(?)[+] Lucene主要有两种文档模型:Document和Fi ...
- VS2008下直接安装使用Boost库1.46.1版本号
Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C ...
- VS2008下直接安装使用Boost库1.46.1版本
Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C++ ...
- boost asio 异步实现tcp通讯
---恢复内容开始--- asioboost 目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化 一.前言 boost asio可算是一个简 ...
- VS2008下直接安装Boost库1.46.1版本号
Boost图书馆是一个移植.提供源代码C++库.作为一个备份标准库,这是C++发动机之间的一种标准化的过程. Boost图书馆由C++图书馆标准委员会工作组成员发起,一些内容有望成为下一代C++标准库 ...
- Boost(1.69.0) windows入门(译)
目录 Boost windows入门 1. 获得Boost源代码 2. Boost源代码组织 The Boost Distribution 3. 仅用头文件的库 Header-Only Librari ...
- boost multi_index简单了解
#include <string> #include <iostream> #include <boost/multi_index_container.hpp> # ...
- elasticsearch——海量文档高性能索引系统
elasticsearch elasticsearch是一个高性能高扩展性的索引系统,底层基于apache lucene. 可结合kibana工具进行可视化. 概念: index 索引: 类似SQL中 ...
随机推荐
- Raspbian 在虚拟机上运行,运行Flask,供宿主机访问
Raspbian 在虚拟机上运行,启动Flask,供宿主机访问 参考ref 1, 在virtualbox上跑起来Raspbian OS 参考ref 2, 在Raspbian上安装并运行Falsk, 注 ...
- python之字符串切分
在工作中,经常遇到字符串切分,尤其是操作linux命令,返回一段文本,如下面这种格式 Filesystem Size Used Avail Use% Mounted on /dev/vda1 40G ...
- CSP-S2019退役记/爆内存记
DAY 0 准备出发. 出发前教练说遇到事不慌,打电话,又听教练说了说历年赶车经历. 然后这趟路上居然没有什么大事. 在车上有些闲,于是就和其他人聊了会天,聊着聊着没意思,就用手机翻博客园. 这样就不 ...
- Security基础(四):OpenSSL及证书服务常用系统监控命令、搭建nagios监控服务器、配置文件及插件使用、监控远程主机的公有数据、监控远程主机的私有数据
一.OpenSSL及证书服务常用系统监控命令 目标: 本案例要求练习常用的系统监控命令完成以下任务: 使用vmstat命令监控内存及磁盘I/O信息 使用iostat命令监控CPU处理器及磁盘的I/O信 ...
- handler消息机制入门
handler消息机制入门 为什么要用handle? 我们在网络上读取图片信息时,是不能把耗时操作放在主线程里面的,当我们在子线程中获取到了图片的消息的时候,我们就需要把这个数据传给主线程. 而直接使 ...
- flask 实现简易图书管理
""" 1.配置数据库 a.导入 SQLalchemy库 b.创建db对象,并配置参数 c.创建数据库 2.添加书和作者的模型 a.模型集成db.Model b.__ta ...
- POI 讀取EXCEL
/*** 獲取單元格數值* * @param cell* @return*/private String getCellValue(Cell cell) {String cellValue = &qu ...
- Topshelf 秒建 Windows 服务
https://www.jianshu.com/p/f2365e7b439c 在服务器上,可cmd cd 进入bin目录下执行
- css缓存问题
频繁更换样式,会导致样式缓存, 在实际项目开发过过程中,页面是上传到服务器上的.而为了减少服务器的压力,让用户少加载,浏览器会将图片.css.js缓存到本地中,以便下次访问网站时使用.这样做不仅减少了 ...
- 关系型数据库MySQL(二)_索引
优点 大大加快数据的查询速度 创建唯一性索引,保证数据库表中每一行数据的唯一性 在使用分组和排序子句进行数据检索时,可以显著减少查询中分组和排序的时间 缺点 索引需要占物理空间 当对表中的数据进行增删 ...