【问题标题】:How to use/create boost::multi_index?如何使用/创建 boost::multi_index?
【发布时间】:2017-01-23 10:08:57
【问题描述】:

有人可以详细解释一下如何使用boost::multi_index 创建多索引映射吗?我在网上看到了很多例子,也看到了 boost 页面,但我无法理解。我想通过多个 int/long 映射类对象指针作为键。有人可以帮助我理解这一点吗?

我有一个类X 和该类的多个属性long longlongintint。我想将属性long longlongintint 存储为映射到 -> 的键。

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

【问题讨论】:

  • 您想要 int 还是 long 作为键?还是同时一个 int 和一个 long?
  • 你需要展示你所坚持的。给出的唯一可能的答案是链接到许多有用的在线示例。见stackoverflow.com/help/mcve
  • 我有一个类 X 和该类的多个属性,它们是 (long long)、(long)、(int)、(int)。我想存储 作为键映射到 ->
  • 只是为了确定:你知道多映射意味着你可能有重复的键,而不是你可能有不同的键指向同一个对象。
  • @AdrianMaire 我相信标题只是用了错误的词;正文是关于 boost::multi_index 的,这里确实适用。

标签: c++ visual-c++ boost multikey


【解决方案1】:

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
};

接下来,我们将为我们希望容器拥有的每个索引准备一个标签。标签不是绝对必要的(索引可以按顺序访问),但为每个索引提供名称更方便:

struct IndexByL {};
struct IndexByI1 {};
struct IndexByI2 {};

现在,我们有了组合容器类型所需的东西:

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
    >
  >
>;

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

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);

[Live example]

现在,一些关于野兽一般如何工作的散文。

您可以通过指定它将存储的对象类型(在您的情况下为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...)一样使用它。通过该索引进行的更改将影响整个容器。

【讨论】:

  • 谢谢!这非常有用。正如您所描述的,我没有将这些属性作为单独的结构。我会将这些属性作为另一个类的成员,X 类或其父类将从该类派生。这会改变你的答案吗?
  • @Ram 不,我的意思是 X 是您正在使用的结构。我会澄清答案。
  • 感谢安吉!为了让自己更清楚,我希望类对象的指针被属于其父/父的父类的类的属性索引/映射。
  • @Ram 基类的成员被派生类继承,所以没有问题。如果您发现无法从其他密钥提取器轻松组合密钥,您始终可以使用global_fun 密钥提取器并进行任意处理。
  • 我可以使用这样的东西吗?这行得通吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2016-03-23
  • 2011-03-29
  • 1970-01-01
  • 2017-01-27
  • 1970-01-01
相关资源
最近更新 更多