Boost Multi Index:基于列表内容的索引
Boost Multi Index: index based on list content
我是Boost多索引容器的新手,想知道它是否能以更有效的方式解决我的问题,简化如下:
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的卫星(这确实是唯一的)
遗憾的是,我没有手在卫星结构,这意味着,我不能改变它。
简单来说就是:
struct Satellite {
int norad_id;
std::list<Transponder> transponders;
std::list<Source> sources;
... some more data
}
我想做的就是简单地搜索任何卫星,并找到具有特定应答器-或源-或norad id的卫星。
目前,我正在使用3个漂亮的地图
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
相关文章:
- 跟随整数索引列表的自定义类迭代器
- 在子集化后将包含索引号的列表列表映射到标准索引序列
- CLI/C++dataGridView示例-设置comobox单元格列表的索引
- 对C++列表的基于索引的访问
- 存储将单个查询图像与多个图像的列表匹配的关键点索引
- C++娜娜 如何从全局列表框*指针访问列表框内的索引 0 上的按钮?
- 在索引中插入节点:链接列表未正确返回列表
- 如何在C 中以给定索引列表中的项目插入项目
- 获得给定向量的排列索引列表的最佳方法是什么?
- 列表和向量的索引之间的差异
- 图像中使用CUDA中的白色像素索引列表
- 如何获得具有多个索引的对象列表
- 用eCmascript Regex验证Python列表索引语法
- 将元素从一个向量复制到另一个向量的最有效方法,给定一个不被复制的索引列表
- 获取类型列表中的类型的索引
- 如何使用QREGEXP解析PACMD列表输出并查找接收器索引和名称
- 非冗余对列表中对象的索引
- C :通过索引从列表中获取对象无效
- 使用值为 std::shared_ptr的映射是具有多索引类列表的良好设计选择
- C++列表索引查询