Lambda expressions are one of the most powerful features in C++. Lambda is introduced in the 11th version of C++ and is also available in C++ versions 14 and 17.
Whenever we need a function pointer in our program, in that particular case, we can use lambda. It provides additional options to the C++ programmer to write a function definition without defining a function. Now we discuss details about the concept of the lambda expression.
Lambda Syntax
The lambda syntax mainly consists of three parts. They are
- []
- ()
- {}
Where [] is called Capture List.
() is called Function Argument.
{} is called Function Body.
The Elements of Lambda
- Capture List: Here, we write lambda expression or function, whatever will be required in the program.
- Parameters: It is also known as lambda declaration. It may be used as an optional case.
- Method Definition: Here, we explain how the function will behave in the program.
Define and Calling a function
{
[] 90 { // Capture list and Parameter.
cout<< “The coding and expression part of Lambda” << endl ;
} () ; // Open and Close brace will invoke in lambda expression.
return 0 ;
}
Programming Example 1
using namespace std ;
struct trippleValue { // Define a Function object .
int operator() ( int value ) {
return value * 3 ;
}
};
int main()
{
trippleValue tv ; // Object Creation .
int t = tv( 2 ) ;
cout<< “ The Cube value is ” << t << endl ;
return 0 ;
}
Output
Explanation
Here we want to explain where lambdas are actually applied to the program. To do this, here, we create a function object named tv. The class name is trippleValue. So we create the object tv of the class trippleValue, which will get a value and turn it into a cube value. Inside the main function, we create an object tv and pass a value of 2. Now print the value of t; it shows the result 8. Because the cube of 2 is 8.
It will be observable that lambdas are not used in the above program. Now the same type of program will be run with the help of lambda expression in a very efficient manner in the next program.
Programming Example 2
using namespace std ;
int main()
{
auto v = [] ( int value ) { return value * 2 ; } ; // Lambda Expression are used here.
int t = v( 2 ) ;
cout<< << t << endl ;
return 0 ;
}
Output
Explanation
Now we have done the same program with the help of lambda expression. Here we declare a variable v which is an auto type variable, where we store the value which lambda expression will return. We do that calculation in a single line. Now we pass a value 2 inside the variable v and store it inside the t variable. In the next line of the program, we simply print the variable t. It shows result 4. As we return a value inside the lambda expression as value * 2.
Programming Example 3
#include<algorithm>
#include<vector>
int main()
{
std :: vector vc { 10 , 20 , 30 , 40 } ;
std :: for_each( vc.begin() , vc end() , [] ( int n) {
cout<< << n * n << endl ;
}) ;
return 0 ;
}
Output
Explanation
Here we take a vector named vc, which takes some values 10, 20, 30, and 40. We want to square each element in the vector. For this, we use for_each loop. Inside the parenthesis of the for_each loop, we use a lambda expression. Inside its body, we simply square these vector values.
Benefits of using lambda
- Lambda expression increases the readability of the program efficiently.
- It reduces the no of lines in the code, and the programs become simpler. As a result, the programmer gets relief to write less no coding.
- Lambda expression does not introduce any extra new name or lines to the program. It increases the robustness of the program.
- It also decreases the time complexity of the program.
Conclusion
Discussing details about the concept of the lambda expression topic in C++, we have come to the conclusion that it is one of the powerful concepts of C++. Through lambda expression, we can easily be done a programming code in an efficient manner. If we are able to understand the concept of lambda and its uses, then with the help of lambda expression, we can solve many programming answers very easily.