From: CornedBee on
On Jun 23, 1:45 am, Mikosz <mikosz...(a)gmail.com> wrote:
> Good point, I actually forgot about 'transform'. However, a solution
> that would allow me not to implement an extracting functor would be
> much neater.

Boost, TR1, or a C++0x standard library provide bind, which can do
this:

-------

std::transform(as.begin(), as.end(), std::inserter(vals,
vals.begin()),
std::bind(&A::val, _1));

-------

Of course, if you're using Boost, you could use a transform_iterator,
which could be nicer, except for the verbose syntax of providing a
transform range:

-------

BOOST_AUTO(pred, std::bind(&A::val, _1));
std::set<int> vals(boost::make_transform_iterator(as.begin(), pred),
boost::make_transform_iterator(as.end(), pred));

-------

So we need ranges. Unfortunately, with concepts we also lost range
constructors for containers. Thus, the Boost.Range version is not as
pretty as it could be:

-------

boost::copy(as | boost::adaptors::transformed(std::bind(&A::val, _1)),
std::inserter(vals, vals.begin()));
// or:
boost::insert(vals, vals.begin(),
as | boost::adaptors::transformed(std::bind(&A::val,
_1)));
// or possibly:
boost::push_back(vals, as |
boost::adaptors::transformed(std::bind(&A::val, _1)));

-------

If we had range constructors, it would simply be this:

------

std::set<int> vals(as |
boost::adaptors::transformed(std::bind(&A::val, _1)));

------

Sebastian


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]