|
Boost Users : |
Subject: [Boost-users] [multi_index] iterating over keys and "extracting" key extractors
From: Nick Stokes (randomaccessiterator_at_[hidden])
Date: 2015-03-18 12:51:41
Q1: Is there a builtin in way in MI to iterate over keys rather than
elements? I.e. the iterator's value type will be that of the key, not the
element.
Q2: Is there a way to obtain the type of key extractors given a multi
index container? (it is related to Q1, as this sort of a thing could be
passed to boost::transform iterator to obtain an alternative answer for Q1)
An example is in order:
struct Foo {
int id;
sting name;
};
using namespace boost::multi_index;
typedef multi_index_container< Foo, indexed_by<
ordered_unique< member< Foo, int, &Foo::id > >,
ordered_unique< member< Foo, string, &Foo::name > >
> MFoo;
MFoo foos;
// .. populate
// Now, we want to iterate over sorted name's only, for e.g:
auto beg = answer_to_Q1(foos).begin();
auto end = answer_to_Q1(foos).end();
std::copy( beg, end, ostream_iterator<string>(cout, " ") );
// Continuing the example for Q2:
auto aFunctorThatExtractsFooName = answer_to_Q2<M>::type();
auto beg = make_transform_iterator( M.get<1>.cbegin(),
aFunctorThatExtractsFooName );
auto end = make_transform_iterator(
M.get<1>.cend(), aFunctorThatExtractsFooName );
std::copy( beg, end, ostream_iterator<string>(cout, " ") );
Thanks!
Nick
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net