문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top