c++ - boost 多索引 : index based on list content

标签 c++ boost

我是 Boost Multi Index 容器的新手,想知道它是否能以更有效的方式解决我的问题,简化如下:

struct A {
   int id;
}

struct B {
   int id;
   std::list<A> products;
}

每个 A 都有一个严格唯一的 ID,我希望能够使用一个多索引容器,能够查找 B。 通过 B 的 ID 和 A 的 ID。

目前我正在使用漂亮的 std::map,并将 A id 映射到 B id。 这么说吧。它工作得很好。但是,我有其他参数来进行这样的查找,而且它变得非常讨厌 :)

编辑:

根据评论请求,我将详细说明:

我有卫星,而卫星又有许多转发器和许多来源。 我希望能够找到给定转发器 ID 或源 ID(确实是唯一的)的卫星

遗憾的是,我没有掌握 Satellite 结构,这意味着我无法更改它。

简而言之,它看起来像这样:

struct Satellite {
 int norad_id;
 std::list<Transponder> transponders;
 std::list<Source> sources;
 ... some more data
}

我想做的是简单地搜索任何卫星,并找到具有特定转发器、源或 norad id 的卫星。

目前,我正在使用 3 个不错的 map

std::map<int /*norad*/ ,Satellite> satellites;
std::map<int /*transponder  id*/, int/* norad */> transponder_to_satellite;
std::map<int /* source_id */,  int /* norad */ > source_to_satellite;

从@sehe 提供的示例中,我发现如果我生成一个关系结构会更容易一些。我想我会试一试……:)

最佳答案

在没有确切用例的情况下,这里有一些建议可以根据您展示的内容对索引进行建模¹

struct Product {
int id;
};

struct Category {
int id;
};

struct ProductCategoryRelation {
    int productId;
    int categoryId;
};

namespace bmi = boost::multi_index;

using RelationTable = bmi::multi_index_container<
    ProductCategoryRelation,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct by_product>,
            bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::productId>
        >,
        bmi::ordered_unique<
            bmi::tag<struct by_category>,
            bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::categoryId>
        >
    >
>;

您还可以使用复合键变得非常聪明,它在 ordered_* 索引中用途广泛:

using RelationTable = bmi::multi_index_container<
    ProductCategoryRelation,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct by_product>,
            bmi::composite_key<ProductCategoryRelation,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::categoryId>,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::productId>
            >
        >
    >
>;

这是一个小演示:

Live On Coliru

#include <boost/multi_index_container.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/composite_key.hpp>
#include <boost/multi_index/global_fun.hpp>
#include <list>

struct Product {
   int id;
};

struct Category {
   int id;
};

struct ProductCategoryRelation {
    int productId;
    int categoryId;
};

namespace bmi = boost::multi_index;

using RelationTable = bmi::multi_index_container<
    ProductCategoryRelation,
    bmi::indexed_by<
        bmi::ordered_unique<
            bmi::tag<struct compound>,
            bmi::composite_key<ProductCategoryRelation,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::categoryId>,
                bmi::member<ProductCategoryRelation, int, &ProductCategoryRelation::productId>
            >
        >
    >
>;

#include <iostream>
#include <boost/range/iterator_range.hpp>

int main() {
    RelationTable table {
        ProductCategoryRelation { 1, 7 },
        ProductCategoryRelation { 2, 7 },
        ProductCategoryRelation { 3, 7 },
        ProductCategoryRelation { 4, 6 },
        ProductCategoryRelation { 5, 6 },
        ProductCategoryRelation { 6, 6 },
        ProductCategoryRelation { 7, 5 },
        ProductCategoryRelation { 8, 5 },
        ProductCategoryRelation { 9, 5 },
    };

    // find all products in category 6:
    for (auto& rel : boost::make_iterator_range(table.get<compound>().equal_range(6)))
        std::cout << "Product " << rel.productId << " is in category " << rel.categoryId << "\n";
}

打印:

Product 4 is in category 6
Product 5 is in category 6
Product 6 is in category 6

¹ 我将类名 Crystal 球化为“现实”的东西

关于c++ - boost 多索引 : index based on list content,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32868291/

相关文章:

c++ - boost::asio 无法从文件中读取超过 65536 个字节

c++ - Boost::Asio with Main/Workers threads - 我可以在发布工作之前启动事件循环吗?

c++ - 如何通过在它之前编写的预处理器定义来获取函数签名?

C++ |使用 setTo 更改 cv::mat 中的颜色

c++ - ios::app 不也暗示 ios::out 在 C++ 中吗?

c++ - 梯度和内存

以字符串为参数的C++调用函数

c++ - 将 char 和 int 的随机序列的字符串解析为单独的 vector

c++ - 在 Windows 7 上使用 cygwin 为 gcc 构建 boost - Bootstrap 错误

c++ - 在逗号上使用多参数模板类型扼流圈的 Boost Fusion 结构