Using the new lambda function of C++ 11
Lambda functions are the new addition to the C++ family. They can be described as anonymous functions.
Getting ready
To work through this recipe, you will need a machine running Windows and Visual Studio.
How to do it…
To understand a lambda function let's have a look at the following code:
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { vector<int> numbers{ 4,8,9,9,77,8,11,2,7 }; int b = 10; for_each(numbers.begin(), numbers.end(), [=](int y) mutable->void { if(y>b) cout<< y<<endl; }); int a; cin >> a; }
How it works…
Lambda functions are a new addition to the C++11 family. They are anonymous functions and can be very handy. They are generally passed as arguments to a function. The syntax of a lambda function is as follows:
[ capture-list ] ( params ) mutable(optional) exception attribute -> ret { body }
The mutable
keyword is...