c++ - boost multi_index 是如何实现的

标签 c++ boost multi-index boost-multi-index

我很难理解 Boost.MultiIndex 是如何实现的。假设我有以下内容:

typedef multi_index_container<
    employee,
    indexed_by<    
        ordered_unique<member<employee, std::string, &employee::name> >,
        ordered_unique<member<employee, int, &employee::age> >
    > 
> employee_set;

我想我有一个数组,Employee[],它实际上存储 employee 对象,以及两个 map

map<std::string, employee*>
map<int, employee*>

以姓名和年龄为键。每个映射都有 employee* 值,该值指向数组中存储的对象。这样可以吗?

最佳答案

给出了底层结构的简短解释here ,引述如下:

该实现基于与指针互连的节点,就像您最喜欢的 std::set 实现一样。我将对此进行详细说明:std::set 通常实现为节点看起来像的 rb-tree

struct node
{
  // header
  color      c;
  pointer    parent,left,right;
  // payload
  value_type value;
};

嗯,multi_index_container 的节点基本上是一个“多节点”,其 header 与索引以及有效负载一样多。例如,具有两个所谓的有序索引的 multi_index_container 使用的内部节点看起来像

struct node
{
  // header index #0
  color      c0;
  pointer    parent0,left0,right0;
  // header index #1
  color      c1;
  pointer    parent1,left1,right2;
  // payload
  value_type value;
};

(现实更复杂,这些节点是通过一些元编程等生成的,但你明白了)[...]

关于c++ - boost multi_index 是如何实现的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4206349/

相关文章:

c++ - 这可以用 C++ 中的模板来完成吗?

C++ 正则表达式字符串捕获

c++ - “boost/iostreams/device/file_descriptor.hpp”文件未找到错误

python - Pandas Multiindex 数据框删除行

c++ - 混淆右值和左值

c++ - 从 C++ 中的方法返回对象

python - 访问 Pandas 中多索引的一级

c++ - 如何使用复合键获取 boost::multi_index_container 中第一个键的不同计数

.net - 理解 C++ .Net 中的 String^

c++ - Boost::带有共享指针的侵入式列表