Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::transform using C++0x lambda expression

How is this done in C++0x?

std::vector<double> myv1; std::transform(myv1.begin(), myv1.end(), myv1.begin(),                std::bind1st(std::multiplies<double>(),3)); 

Original question and solution is here.

like image 716
Steve Townsend Avatar asked Oct 07 '10 19:10

Steve Townsend


People also ask

Does C have lambda expression?

No, C doesn't have lambda expressions (or any other way to create closures). This is likely so because C is a low-level language that avoids features that might have bad performance and/or make the language or run-time system more complex.

What does std :: transform do?

std::transform. std::transform applies the given function to a range and stores the result in another range, keeping the original elements order and beginning at d_first . 1) The unary operation unary_op is applied to the range defined by [first1, last1) .

What does [=] mean in C++ lambda?

The [=] you're referring to is part of the capture list for the lambda expression. This tells C++ that the code inside the lambda expression is initialized so that the lambda gets a copy of all the local variables it uses when it's created.

Can std :: function store lambda?

Instances of std::function can store, copy, and invoke any CopyConstructible Callable target -- functions, lambda expressions, bind expressions, or other function objects, as well as pointers to member functions and pointers to data members.


1 Answers

std::transform(myv1.begin(), myv1.end(), myv1.begin(),     [](double d) -> double { return d * 3; }); 
like image 150
Edward Strange Avatar answered Sep 21 '22 11:09

Edward Strange