【问题标题】:boost::multi_index_container MRUboost::multi_index_container MRU
【发布时间】: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 并找到相同的项目是一种选择,但非常丑陋且效率低下。 还有其他方法吗?

【问题讨论】:

标签: c++ boost boost-multi-index


【解决方案1】:

要在不同索引的迭代器之间进行转换,请使用projection

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-06
    • 1970-01-01
    • 2014-12-02
    • 2010-12-14
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    相关资源
    最近更新 更多