【问题标题】:Boost multi index ordered iterators assignment提升多索引有序迭代器分配
【发布时间】:2014-01-07 21:54:00
【问题描述】:

我有一个 boost multi_index_container 存储一堆具有以下索引的位置

boost::multi_index::indexed_by<
    boost::multi_index::ordered_unique<
            boost::multi_index::tag<Slot>,
            boost::multi_index::identity<SlotData>
        >,  //ordered_unique
    boost::multi_index::ordered_non_unique<
            boost::multi_index::tag<Level>,
            SlotData::ExtractZ
        >,  // ordered_non_unique
...//index by
.//typedef as SlotLocations

在这个定义中,我相信默认索引将是基于ordered_uniqueSlot。稍后我有一个迭代器SlotLocations::iterator,我想用它来存储基于Level(即ordered_non_unique)的搜索结果:

typedef SlotLocations::index<Level>::iterator MIterator;

std::pair<MIterator, MIterator> range = map.get<Level>().range(..some conds..);

SlotLocations::iterator itr = range.first;    //error

范围搜索有效并返回存储在range 中的所需结果,但是此代码无法编译,带有标记的行会出现以下错误:

error: no match for 'operator='

为什么我不能像这样分配/存储迭代器?以及存储搜索结果迭代器的可能方式是什么?

【问题讨论】:

    标签: c++ boost multi-index


    【解决方案1】:

    您必须跨索引投影迭代器

    应该是这样的

     SlotLocations::iterator itr = project<Slot>(map, range.first);
    

    【讨论】:

    • 干杯,错过了。为什么 boost 不为此实现自动“强制转换”?
    • @xmrflipflop 我想你的意思是隐式转换?好吧,一方面,未标记的索引可能具有相同的类型(这可以通过更多 TMP 来解决)。但更重要的是,隐式转换往往会在更复杂的环境中崩溃或导致难以诊断的意外情况。
    【解决方案2】:

    关于为什么不能通过隐式转换自动完成投影的第二个问题:实际上原因是 不是 @sehe 建议的那个(所有索引都是不同的,即使未标记,它们的迭代器肯定是不同的类型),但是:投影是通过像

    这样的表达式完成的
    it1 =  c.project<X>(it0); // X is either a tag or an index number
    

    需要用户提供两个迭代器所属的容器:也就是说,您不能(通常)仅使用it0 持有的信息将it0 转换为it1 .

    【讨论】:

      猜你喜欢
      • 2020-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-12
      相关资源
      最近更新 更多