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中 ...
随机推荐
- Ext js-02 -官方API文档使用
官方API文档地址: http://docs.sencha.com/extjs/6.5.3/classic/Ext.html 打开网页如下: 1.选择所使用的Ext js版本,后面offline do ...
- 编写现代 CSS 代码的 20 个建议
明白何谓Margin Collapse 不同于其他很多属性,盒模型中垂直方向上的Margin会在相遇时发生崩塌,也就是说当某个元素的底部Margin与另一个元素的顶部Margin相邻时,只有二者中的较 ...
- C#操作Access的查询、添加、删除、修改源程序
C#操作Access的查询.添加.删除.修改源程序 using System; using System.Collections.Generic; using System.ComponentMode ...
- JS当中的无限分类递归树
列表转换成树形结构方法定义: //javascript 树形结构 function toTree(data) { // 删除 所有 children,以防止多次调用 data.forEach(func ...
- ArrayList和Map的一些知识
2014年10月17日16:39:01 1.如何获取某条数据在ArrayList中的索引值? 多个对象存储的ArrayList中,如果只知道对象其中的一个属性,如何获取该对象在ArrayList里的索 ...
- 重新认识new
前言 感谢大佬:https://www.cnblogs.com/luxiaoxun/archive/2012/08/10/2631812.html www.cplusplus.com 因为这段时间在重 ...
- 【VisualStdio】在VS2015中显示上下文菜单中“创建单元测试”菜单
---恢复内容开始--- VS2012以后创建单元测试的选项被默认隐藏了,创建单元测试变得无比低效率.看msdn的说法好像是想推荐使用Intell Test来替代单元测试的用途,但是还没摸清楚也不敢瞎 ...
- How many groups(DP)
题意: 定义:设M为数组a的子集(元素可以重复),将M中的元素排序,若排序后的相邻两元素相差不超过2,则M为a中的一个块,块的大小为块中的元素个数 给出长度为n的数组a,1<=n<=200 ...
- [题解]Magic Line-计算几何(2019牛客多校第三场H题)
题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意: 给你偶数个点的坐标,找出一条直线将这n个点分成数量相等的两部分 并在这条直线上取不同的两个点,表示 ...
- MybatisPlus自动填充公共字段的策略
背景:数据库中多个表有时间字段,并且字段名一致 需求:该时间字段由MybatisPlus自动插入和更新,业务代码无需处理 方法: 一.创建基础实体[BaseEntity],定义需要处理的公共字段(创建 ...