Even after studying the examples, I'm having trouble figuring out how to extract ranges using a composite key on a MultiIndex container.
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;
My goal is to get an iterator pointing to the first of the subset of elements in HostContainer hmap
that has age partnerAge
and returns false
to Host::isPaired()
:
std::pair< hmap::iterator,hmap::iterator > pit = hmap.equal_range(boost::make_tuple( partnerAge, false ) );
I think this is very wrong.
std::pair
? (I'm copying syntax from an example that I don't understand.)
std::count
to calculate the number of elements of age partnerAge
that are eligible (return false
to Host::isPaired()
). What is the syntax for extracting the sorted index that meets these requirements?
I'm obviously still learning C++ syntax. Thanks in advance for any help.
To get access to N-th index you could use function get
as follows:
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
returns pair of iterators of N-th index. You will get range of elements that are satisfy the specified condition since your composite key is not unique. To iterate through that range you could use loop from the first iterator to the second iterator.
Consider using named indexes to use them as get<index_name>()
, because specifying actual number is not very readable.
count
has syntax similar to equal_range
:
size_t cnt = hmap.get<3>().count(boost::make_tuple( partnerAge, false ) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With