consider this code snippet : iteration over one container of a first type T1 for creating a second container of a second type T2 applying a transformation function T1->T2 but only for T1 elements verifying a predicate (T1 -> bool )
(is Odd in the following example).
std::vector<int> myIntVector;
myIntVector.push_back(10);
myIntVector.push_back(15);
myIntVector.push_back(30);
myIntVector.push_back(13);
std::vector<std::string> myStringVectorOfOdd;
std::for_each(myIntVector.begin(), myIntVector.end(),
[&myStringVectorOfOdd](int val)
{
if (val % 2 != 0)
myStringVectorOfOdd.push_back(std::to_string(val));
});
What I don't like in this code is the capture on the lambda. Is there a way to combine std::copy_if and std::transform to achieve the same result in a more elegant and concise way ?