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.
- 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::countto calculate the number of elements of agepartnerAgethat are eligible (returnfalsetoHost::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
getas follows:equal_rangereturns 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.counthas syntax similar toequal_range: