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.
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.
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) .
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.
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.
std::transform(myv1.begin(), myv1.end(), myv1.begin(), [](double d) -> double { return d * 3; });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With