2016-12-02 49 views
0

我的代码(不是我写的)使用多索引容器。boost multi_multi索引的用法

typedef boost::multi_index_container< 
     Block*, 
     boost::multi_index::indexed_by< 
      boost::multi_index::random_access<>, 
      boost::multi_index::ordered_non_unique<BOOST_MULTI_INDEX_MEMBER(Block,uint,pages_invalid) > 
     > 
    > active_set; 

typedef active_set::nth_index<0>::type ActiveBySeq; 
typedef active_set::nth_index<1>::type ActiveByCost; 
active_set active_cost; 

“块”是一类。

该容器被用作优先级队列。

ActiveByCost::iterator it = active_cost.get<1>().end(); 
    --it; 

我想搜索变量A ==“特定值”的成员。 (不适用于优先队列)

A是Class Block的成员变量之一。

有没有办法做到这一点?

回答

0

我喜欢搜索具有变量A的成员会==“特殊价值”

如果该成员上没有索引,那么这将需要在随机存取线性搜索index:

auto pos = std::find_if(active_cost.begin(), active_cost.end(), 
         [](Block const* p) { return p->A == "specific value"; } 
if(pos == active_cost.end()) 
    // Not found. 
+0

是否“如果这个成员没有索引”意味着A不是数组? – WKK

+0

@WKK这意味着'boost :: multi_index :: indexed_by <>'中没有该成员的索引。 –

+0

@maxim_egorushkin谢谢。还有一个问题,我如何访问multi_index容器的位置数据? – WKK