gpt4 book ai didi

c++ - Boost MultiIndex 中的复合键语法

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:37:06 24 4
gpt4 key购买 nike

即使在研究了 examples 之后,我无法弄清楚如何使用 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;

我的目标是获得一个迭代器,该迭代器指向 HostContainer hmap 中具有年龄 partnerAge 的第一个元素子集并返回 falseHost::isPaired():

  std::pair< hmap::iterator,hmap::iterator > pit = hmap.equal_range(boost::make_tuple( partnerAge, false ) );

我认为这是非常错误的。

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

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

最佳答案

要访问第 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 ) );

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

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

count语法类似于 equal_range :

size_t cnt = hmap.get<3>().count(boost::make_tuple( partnerAge, false ) );

关于c++ - Boost MultiIndex 中的复合键语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2669770/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com