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的更多相关文章

  1. boost multi array

    Boost MultiArray is a library that simplifies using arrays with multiple dimensions. 1. #include < ...

  2. 基础:从概念理解Lucene的Index(索引)文档模型

    转:http://blog.csdn.net/duck_genuine/article/details/6053430   目录(?)[+]   Lucene主要有两种文档模型:Document和Fi ...

  3. VS2008下直接安装使用Boost库1.46.1版本号

    Boost库是一个可移植.提供源码的C++库,作为标准库的后备,是C++标准化进程的发动机之中的一个. Boost库由C++标准委员会库工作组成员发起,当中有些内容有望成为下一代C++标准库内容.在C ...

  4. VS2008下直接安装使用Boost库1.46.1版本

    Boost库是一个可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一. Boost库由C++标准委员会库工作组成员发起,其中有些内容有望成为下一代C++标准库内容.在C++ ...

  5. boost asio 异步实现tcp通讯

    ---恢复内容开始--- asioboost   目录(?)[-] 一前言 二实现思路 通讯包数据结构 连接对象 连接管理器 服务器端的实现 对象串行化   一.前言 boost asio可算是一个简 ...

  6. VS2008下直接安装Boost库1.46.1版本号

    Boost图书馆是一个移植.提供源代码C++库.作为一个备份标准库,这是C++发动机之间的一种标准化的过程. Boost图书馆由C++图书馆标准委员会工作组成员发起,一些内容有望成为下一代C++标准库 ...

  7. Boost(1.69.0) windows入门(译)

    目录 Boost windows入门 1. 获得Boost源代码 2. Boost源代码组织 The Boost Distribution 3. 仅用头文件的库 Header-Only Librari ...

  8. boost multi_index简单了解

    #include <string> #include <iostream> #include <boost/multi_index_container.hpp> # ...

  9. elasticsearch——海量文档高性能索引系统

    elasticsearch elasticsearch是一个高性能高扩展性的索引系统,底层基于apache lucene. 可结合kibana工具进行可视化. 概念: index 索引: 类似SQL中 ...

随机推荐

  1. 4412 Linux设备总线

    总线_设备_驱动注册流程详解 注册流程图 • 设备一般都需要先注册,才能注册驱动– 现在越来越多的热拔插设备,反过来了.先注册驱动,设备来了再注册 设备 • 本节使用的命令– 查看总线的命令#ls / ...

  2. Android工作两年之后的第一个App--天真无谐

    一.前言 好长时间没写blog了,主要还是工作上的事有点多,周末又得在家开发自己的app,所以时间真的不够用了,当然今天这篇文章主要就要说一下,工作两年的我如何从产品角度去做一个app,以及app的发 ...

  3. [ZJOI2019]开关

    以下的方案数默认是带权方案数.设\(P=\sum_{i=1}^np_i\). 设\(F(x)\)为按\(i\)次开关后到达终止态的方案数的EGF,\(f\)为\(F\)的OGF,显然\(F(x)=\p ...

  4. java 节点流(字符流,字节流)和包装流(缓冲流,转换流)

    结点流:直接对File类进行操作的文件流 package stream; import java.io.File; import java.io.FileNotFoundException; impo ...

  5. 前端每日实战:99# 视频演示如何用纯 CSS 创作一个过山车 loader

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/KBxYZg/ 可交互视频 此视频是 ...

  6. python利用eval方法提升dataframe运算性能

    eval方法可以直接利用c语言的速度,而不用分配中间数组,不需要中间内存的占用. 如果包含多个步骤,每个步骤都要分配一块内存 import numpy as npimport pandas as pd ...

  7. (一)arm交叉编译工具链准备

    1.背景 arm机器一般因为资源问题进行编译会影响开发速度,而且很多时候因为资源不够而无法完成编译工作.因此,需要在执行机上进行交叉编译,即使用x86或其他架构机器基于交叉编译工具编译出在arm上可以 ...

  8. MySQL &lt; &gt; 等用法

  9. html常用代码

    <marquee width="70%" scrollamount="2">大家好</marquee>    // 大家好 字符从左到右 ...

  10. 关于Http请求Cookie问题

    在Http请求中,很多时候我们要设置Cookie和获取返回的Cookie,在这个问题上踩了一个很大的坑,主要是两个问题: 1.不能获取到重定向返回的Cookie: 2.两次请求返回的Cookie是相同 ...