lambda functions
In C++, a lambda function is a feature introduced in C++11 that allows you to define small, anonymous functions directly within the code.
Lambda functions are convenient when you need a simple function for a short period, and you don’t want to create a separate named function.
They are especially useful when used with algorithms, as they allow you to define custom operations inline, making the code more concise and readable.
Table of Contents
Syntax of Lambda Function:
The syntax of a lambda function follows this general form:
[ captures ] ( parameters ) -> return_type {
// function body
}
captures
: Specifies variables from the enclosing scope that the lambda function can access. This can be used to capture variables by reference (&
) or by value (=
).parameters
: Specifies the input parameters of the lambda function, similar to regular function parameters.return_type
: Specifies the return type of the lambda function. If not provided, the return type is deduced automatically.
Example of Lambda Function:
Let’s demonstrate a simple example of using a lambda function to filter even numbers from a vector.
Code:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Use a lambda function to check if a number is even
auto isEven = [](int num) { return num % 2 == 0; };
// Use the std::remove_if algorithm with the lambda function to remove odd numbers
numbers.erase(std::remove_if(numbers.begin(), numbers.end(), [](int num) { return !isEven(num); }), numbers.end());
// Print the filtered vector containing only even numbers
for (int num : numbers) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
Output:
2 4 6 8 10
Explanation of the Code:
In this example, we use a lambda function to check if a number is even or not. The lambda function takes an integer argument num
and returns true
if the number is even; otherwise, it returns false
.
We then use the std::remove_if
algorithm along with the lambda function to filter even numbers from the numbers
vector. The remove_if
algorithm rearranges the elements in the vector so that all elements satisfying the condition (i.e., odd numbers) are moved to the end of the vector. It returns an iterator pointing to the first element beyond the valid range, which is then used with the erase
method to remove the unwanted elements from the vector.
As a result, the vector numbers
contains only even numbers after filtering, which are then printed in the output.
Advantages of Lambda Functions:
- Concise code: Lambda functions allow you to define small functions directly within the code, reducing the need for creating separate named functions.
- Simplified syntax: The syntax of lambda functions is lightweight and easy to read, especially for simple operations.
- Customization: Lambda functions can capture variables from the enclosing scope, making it easy to customize their behavior.
Disadvantages of Lambda Functions:
- Limited reusability: Lambda functions are typically used for short-lived tasks, and their anonymous nature makes them less suitable for complex, reusable functions.
- Code readability: Using complex lambda functions can make the code harder to read and maintain, so it’s essential to strike a balance between conciseness and readability.