vendredi 26 octobre 2018

How to use std::transform with a lambda function that takes additional parameters

In C++ 11 (or higher) can I use std::transform and a lambda function to transform a vector that also takes other parameters? For example, how do I pass param to the lambda function below?

std::vector<double> a{ 10.0, 11.0, 12.0 };
std::vector<double> b{ 20.0, 30.0, 40.0 };
std::vector<double> c;
double param = 1.5;
//The desired function is c = (a-b)/param   
transform(a.begin(), a.end(), b.begin(), std::back_inserter(c),
          [](double x1, double x2) {return(x1 - x2)/param; });

std::transform wants a function with two input parameters. Do I need to use std::bind?

Aucun commentaire:

Enregistrer un commentaire