Boost MultiIndex中的复合键语法

Sar*_*rah 5 c++ boost

即使在研究了这些例子之后,我也无法弄清楚如何使用MultiIndex容器上的复合键来提取范围.

typedef multi_index_container<
  boost::shared_ptr< Host >,
  indexed_by< 
    hashed_unique< const_mem_fun<Host,int,&Host::getID> >, // ID index
    ordered_non_unique< const_mem_fun<Host,int,&Host::getAgeInY> >, // Age index
    ordered_non_unique< const_mem_fun<Host,int,&Host::getHousehold> >, // Household index
    ordered_non_unique< // Age & eligibility status index
      composite_key<
         Host,
        const_mem_fun<Host,int,&Host::getAgeInY>,
        const_mem_fun<Host,bool,&Host::isPaired>
        >
       >
    > // end indexed_by
  > HostContainer;
Run Code Online (Sandbox Code Playgroud)

我的目标是让一个迭代器指向HostContainer hmap具有age 的元素子集中的第一个partnerAge并返回falseHost::isPaired():

  std::pair< hmap::iterator,hmap::iterator > pit = hmap.equal_range(boost::make_tuple( partnerAge, false ) );
Run Code Online (Sandbox Code Playgroud)

我认为这是非常错误的.

  1. 如何/在何处指定迭代器索引(年龄和资格应为3)?我将来会包含其他复合键.
  2. 这两个迭代器究竟是什么std::pair?(我正在从一个我不理解的例子中复制语法.)
  3. 我会用理想std::count来计算的时代元素的数量partnerAge有资格(返回falseHost::isPaired()).提取满足这些要求的排序索引的语法是什么?

我显然还在学习C++语法.在此先感谢您的帮助.

Kir*_*sky 4

要访问第 N 个索引,您可以使用get如下函数:

std::pair< hmap::nth_index<3>::type::const_iterator,
           hmap::nth_index<3>::type::const_iterator > pit = 
  hmap.get<3>().equal_range(boost::make_tuple( partnerAge, false ) );
Run Code Online (Sandbox Code Playgroud)

equal_range返回第 N 个索引的迭代器对。由于您的组合键不是唯一的,您将获得满足指定条件的元素范围。要迭代该范围,您可以使用从第一个迭代器到第二个迭代器的循环。

考虑使用命名索引将它们用作get<index_name>(),因为指定实际数字的可读性不太好。

count语法类似于equal_range

size_t cnt = hmap.get<3>().count(boost::make_tuple( partnerAge, false ) );
Run Code Online (Sandbox Code Playgroud)