c++ boost range adaptor multiple vectors

796 views Asked by At

I have just recently started using the range adaptor in boost when I had to perform a transform/function on a vector. Below is a snippet of one of the simplest example I came across when starting to use the range adaptor.

int multiplyByTwo(int n) { return n*2; }
std::vector<int> num = {1, 2, 3, 4, 5};
auto result = num | boost::adaptors::transformed(multiplyByTwo);

What if my function requires two arguments instead of one, are there any ways I can pass in two vectors into the range adaptor? For example, in this situation:

int multiplyBoth(int n1, int n2) {return n1*n2; }
std::vector<int> num1 = {1, 2, 3, 4, 5};
std::vector<int> num2 = {1, 2, 3, 4, 5};

Will I still be able to pipe both vectors num1 and num2 into my function through a range adaptor? Perhaps something like this:

auto result = num1 | num2 | boost::adaptors::transformed(multiplyBoth);
1

There are 1 answers

3
Sebastian Redl On

You can use combine to turn multiple ranges into a range of tuples.

You need to adapt your function so that it can handle a tuple, but a lambda can do that.