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 &开发者_C百科gt; pit = hmap.equal_range(boost::make_tuple( partnerAge, false ) );
I think this is very wrong.
- How/Where do I specify the iterator index (which should be 3 for age & eligibility)? I will include other composite keys in the future.
- What exactly are the two iterators in
std::pair
? (I'm copying syntax from an example that I don't understand.) - I would ideally use
std::count
to calculate the number of elements of agepartnerAge
that are eligible (returnfalse
toHost::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 ) );
精彩评论