The most asked-for feature added in C++ in recent years is lambda expressions, which enable us to create the inline anonymous functions.The syntax can seem a bit confusing at first glance, but as you master their use, you will find they provide powerful functionality that can make your source code easier to understand.
A lambda expression resembles a statement and can be defined in a single line of code. The expression is stored in a variable and called like a function.
I have an array of int and I want to traverse on this array and print all ints using STL algorithm std::for_each. Let’s do that using a function pointer,
Let’s see the above example with lambda functions,
Case 1:
Just check the example below which shows how to use outer scope elements inside the lambda functions.
That's all for now !!
A lambda expression resembles a statement and can be defined in a single line of code. The expression is stored in a variable and called like a function.
auto multiply = [](int x, int y) -> int { return x * y; }; int sum = multiply(7, 17); std::cout << "Total: " << sum << std::endl;A lambda expression normally does not have access to the variables defined in the scope that encloses the expression. You can change this with the last part of the expression to be put to work: the [ ] square brackets, which are called the capture block.
int x = 7; auto multiply = [x](int y) -> int { return x * y; }; int sum = multiply(17); std::cout << "Total: " << sum << std::endl;Need of Lambda functions
I have an array of int and I want to traverse on this array and print all ints using STL algorithm std::for_each. Let’s do that using a function pointer,
#include <iostream> #include <algorithm> void display(int a) { std::cout<<a<<" "; } int main() { int arr[] = { 1, 2, 3, 4, 5 }; std::for_each(arr, arr + sizeof(arr) / sizeof(int), &display); std::cout << std::endl; }To do a simple display we created a separate function. Is there any way through which we can achieve our requirement and also avoid this overhead, Lambda functions are the solutions.
Let’s see the above example with lambda functions,
#include <iostream> #include <algorithm> int main() { int arr[] = { 1, 2, 3, 4, 5 }; std::for_each(arr, arr + sizeof(arr) / sizeof(int), [](int x) { std::cout<<x<<" "; }); std::cout << std::endl; }To pass outer scope elements inside lambda functions we can use 2 ways:
Case 1:
[=](int &x) { // All outer scope elements has been passed by value }Case 2:
[&](int &x) { // All outer scope elements has been passed by reference }
Just check the example below which shows how to use outer scope elements inside the lambda functions.
#include <algorithm> int main() { int arr[] = { 1, 2, 3, 4, 5 }; int add = 3; std::for_each(arr, arr + sizeof(arr) / sizeof(int), [&](int x) { std::cout<<x<<" "; // Can modify the add inside this lambda function because // all outer scope elements has write access here. add = 3; }); std::cout << std::endl; std::for_each(arr, arr + sizeof(arr) / sizeof(int), [=](int &x) { x= x + add; // Can not modify the add inside this lambda function because // all outer scope elements has read only access here. }); std::cout << std::endl; std::for_each(arr, arr + sizeof(arr) / sizeof(int), [](int x) { // No access to add inside this lambda function because // all outer scope elements are not visible here. //std::cout<<add<<" "; }); std::cout << std::endl; }
That's all for now !!
No comments:
Post a Comment