Using the Lambda expression for a tiny function

Imagine we have a tiny one-line function that we invoke only once. It's better if we write the operation of that function directly when we need it. We actually had this function in our previous example when discussing the C++ Standard Library. Just go back to the for_each.cpp file and we will find the PrintOut() function that is only invoked once by for_each(). We can make this for_each loop more readable if we use Lambda. Let's take a look at the following code snippet to examine how we refactor the for_each.cpp file:

    /* lambda_tiny_func.cpp */    #include <vector>    #include <algorithm>    #include <iostream>    #include "../vehicle/vehicle.h"    using namespace std;    auto main() -> int    { cout << "[lambda_tiny_func.cpp]" ...

Get Learning C++ Functional Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.