Boost logo

Boost :

From: Neal D. Becker (ndbecker2_at_[hidden])
Date: 2004-10-29 13:34:02


I'm always writing (e.g.)

std::vector<double> out (size);
std::transform (in.begin(), in.end(), out.begin(), func);

Wouldn't it be nice to have versions of the std algorithms that worked like:

template<typename out_cont_t, typename in_cont_t, typename UnaryOperation>
inline out_cont_t transform_r (const in_cont_t& in, UnaryOperation u) {
  out_cont_t out (boost::size (in));
  std::transform (boost::begin (in), boost::end (in), boost::begin (out),
u);
  return out;
}

Then you can say:

std::vector<double> out = transform_r<std::vector<double> > (in, func);

That is, the first template parameter is the desired return container type,
and the algorithm constructs the return value into it.

Since constructors can be elided, I think this may be just as efficient as
the original, or maybe better.

OK, perhaps it looks like I didn't save much typing, but when you chain
function calls you save a lot.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk