开发者

Composite key syntax in Boost MultiIndex

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.

  1. How/Where do I specify the iterator index (which should be 3 for age & eligibility)? I will include other composite keys in the future.
  2. What exactly are the two iterators in std::pair? (I'm copying syntax from an example that I don't understand.)
  3. I would ideally use 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 ) );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜