Question

Suppose I have two std::vectors x and y and a binary function F. I would like to create a new object z (not necessarily a vector), with the property that the ith elemenent of z will be the application of F to the ith elements of x and y. For the time being I use

boost::range::transform(x, y, z.begin(), F)

which of course requires memory allocation for z. My goal however is to avoid this memory allocation and have the transformation lazy evaluated, similarly to what boost::adaptors::transformed does with unary functions. In other words, the type of z would be some type of boost::range. Is there any easy way to do this or would I have to write my own range class? Thanks in advance!

Was it helpful?

Solution

The problem is that Boost.Range does not provide a zip_range that can zip x and y as the inputfor your transform. So you need to write one yourself, e.g. using make_iterator_range and zip_iterator (from the Boost.Iterator library). See e.g. this answer for a code example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top