如何使用/创建boost :: multi_index?

qui*_*raw 8 c++ boost visual-c++ multikey

有人可以详细解释如何使用boost::multi_index?创建多索引地图?我在网上看到很多例子,还有提升页面,但我无法理解.我想将多个int/longs的类对象指针映射为键.有人可以帮我理解这个吗?

我有一个类X,哪些是类的多个属性long long,long,int,int.我想存储的属性long long,long,int,int作为键映射到- > <指针X>.

我希望能够查找给定任何属性的指针.某些属性对于每个对象都是唯一的,有些属性X不是唯一的.

Rei*_*ica 15

Boost.Multi-index提供了一个极其可定制的界面,代价是提供极其复杂的界面,因此很容易理解为什么你会被卡住.

我将提供一个与您的用例匹配的注释示例.

首先,我们的数据:

struct X
{
  long long l; // assume unique
  int i1; // assume unique
  int i2; // assume non-unique
  // plus any ohter data you have in your class X
};
Run Code Online (Sandbox Code Playgroud)

接下来,我们将为每个我们希望容器拥有的索引准备一个标记.标签不是严格必需的(索引可以通过它们的顺序访问),但为每个索引提供一个名称更方便:

struct IndexByL {};
struct IndexByI1 {};
struct IndexByI2 {};
Run Code Online (Sandbox Code Playgroud)

现在,我们需要将容器的类型放在一起:

using Container = boost::multi_index_container<
  X*, // the data type stored
  boost::multi_index::indexed_by< // list of indexes
    boost::multi_index::hashed_unique<  //hashed index over 'l'
      boost::multi_index::tag<IndexByL>, // give that index a name
      boost::multi_index::member<X, long long, &X::l> // what will be the index's key
    >,
    boost::multi_index::ordered_unique<  //ordered index over 'i1'
      boost::multi_index::tag<IndexByI1>, // give that index a name
      boost::multi_index::member<X, int, &X::i1> // what will be the index's key
    >,
    boost::multi_index::hashed_non_unique<  //hashed non-unique index over 'i2'
      boost::multi_index::tag<IndexByI2>, // give that index a name
      boost::multi_index::member<X, int, &X::i2> // what will be the index's key
    >
  >
>;
Run Code Online (Sandbox Code Playgroud)

就是这样,我们有一个容器.接下来,我们将如何使用它:

Container c;  // empty container
X x1{...}, x2{...}, x3{...};  // some data

// Insert some elements
auto& indexByL = c.get<IndexByL>(); // here's where index tags (=names) come in handy
indexByL.insert(&x1);
indexByL.insert(&x2);
indexByL.insert(&x3);

// Look up by i1
auto& indexByI1 = c.get<IndexByI1>();
auto itFound = indexByI1.find(42);
if (itFound != indexByI1.end())
{
  X *x = *itFound;
}

// Look up by i2
auto& indexByI2 = c.get<IndexByI2>();
size_t numberOfHundreds = indexByI2.count(100);
Run Code Online (Sandbox Code Playgroud)

[实例]

而现在,一些关于野兽如何运作的散文.

您可以通过指定它将存储的对象类型(X*在您的情况下)以及可用于访问存储对象的一个​​或多个索引来定义多索引容器.将索引视为用于访问数据的接口.

索引可以是不同的类型:

  • 基于按键排序的索引(思考std::setstd::map)
  • 基于按键排名的索引(想想同样加上对第n个元素的轻松访问)
  • 基于散列键的索引(想想std::unordered_setstd::unordered_map)
  • 基于稳定顺序访问的索引(想想std::list)
  • 基于稳定顺序的随机访问的索引(想想std::vector)

基于键的索引也可以是唯一的(如std::map),也可以是非唯一的(如std::multimap).

定义容器时,将要作为一个模板参数的每个索引传递给boost::multi_index::indexed_by.(在我们上面的例子中,我添加了三个索引).

对于不使用密钥的索引(稳定顺序和随机访问),不需要指定任何内容; 你只是说"我想要这样一个索引."

对于基于键的索引,还需要指定如何从数据中获取密钥.这就是关键提取器发挥作用的地方.(这些是boost::multi_index::member示例中的三种用法).基本上,对于每个索引,您提供一个配方(或算法),用于从存储在容器中的数据派生密钥.目前可用的密钥提取器是:

  • 使用元素本身: identity
  • 使用元素的数据成员: member
  • 使用元素的(常量)成员函数: [const_]mem_fun
  • 使用全局函数: global_fun
  • 将多个密钥提取器合并为一个: composite_key

请注意,密钥提取器能够透明地取消引用指针.也就是说,如果您的数据元素是指向类的指针C,则可以在类上指定关键提取器,C并且取消引用将自动发生.(此属性也在示例中使用).

这样就定义了一个带索引的容器.要访问索引,请get在容器上调用成员函数模板.您可以在模板参数列表中按顺序编号引用索引indexed_by.但是,为了更易读操作,您可以为每个索引(或者只是其中一些索引)引入标记.标记是任意类型(通常是具有合适名称的空结构),它允许您将该类型用作模板参数,get而不是索引的序列号.(这也在示例中使用).

一旦从容器中检索对索引的引用,就可以像索引所对应的数据结构一样使用它(map,hashset,vector,...).通过该索引完成的更改将影响整个容器.