1

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);
AndyG
  • 39,700
  • 8
  • 109
  • 143
Zen
  • 283
  • 1
  • 3
  • 4

1 Answers1

1

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.

Sebastian Redl
  • 69,373
  • 8
  • 123
  • 157
  • 1
    I think it would be nice to add a code sample to this answer to show how to use `combine` and the lambda to achieve OPs goal. [I made one here](https://wandbox.org/permlink/Ol8WpGGSvn8sB6Qc) if you want to just use that. – AndyG Nov 29 '17 at 13:39
  • Thanks Sebastian. I thought of boost::combine but was thinking it would require me to change my function input arguments into a tuple. It will be great if someone can show me some examples of using a tuple along with range adaptors so I dont have to change my function signatures. – Zen Nov 29 '17 at 20:05
  • Thanks Andy! Only saw your comments after asking for an example! That does exactly what I was hoping for! – Zen Nov 29 '17 at 20:11