【发布时间】:2020-05-14 10:52:28
【问题描述】:
我有一个用于 MRU 的 bmi multi_index_container,定义如下
bmi::multi_index_container<Item, bmi::indexed_by<bmi::sequenced<>, bmi::hashed_unique<bmi::tag<hashed>, KeyExtractor>>>
它实际上是基于 bmi example。一旦插入并将项目重新定位到头部,一切都很好。当我通过一个键查找并按以下方式检索它时,问题就开始了。
item_type & get(const key_type & key) const
{
auto item = items.template get<hashed>().find(key);
if (item == items.template get<hashed>().end())
throw std::logic_error("Item not found");
//relocate the item to the head
return const_cast<item_type &>(*item);
}
因为我是通过键来查找它,所以我使用散列索引。然后我必须在sequenced 上调用relocate,但是来自另一个索引的迭代器当然不起作用。当然,遍历sequenced 并找到相同的项目是一种选择,但非常丑陋且效率低下。
还有其他方法吗?
【问题讨论】:
-
迭代器投影在您的情况下不起作用吗? boost.org/doc/libs/1_73_0/libs/multi_index/doc/reference/…
-
哇!没有意识到这一点。将立即检查
-
@IgorR。它按预期工作。谢谢!请将您的评论转换为答案
标签: c++ boost boost-multi-index