In C++, a lambda function (also known as a lambda expression or anonymous function)
is a way to define and create a function without giving it a name.
A lambda function is defined by the following syntax:
Copy code
[capture list](parameter list) -> return type { function body }
The capture list is used to capture variables from the surrounding scope, the
parameter list is used to define the input parameters of the function, the return
type is used to define the return type of the function, and the function body is
used to define the code that will be executed when the function is called.
Here is an example of a lambda function that takes two integers as input and
returns their sum:
Copy code
auto add = [](int x, int y) -> int { return x + y; };
std::cout << add(3, 4) << std::endl; // prints 7
In this example, the lambda function add takes two integers as input and returns
their sum. The type of the lambda function is inferred by the compiler from the
return type and the parameter types. The auto keyword is used to store the lambda
function in a variable.
Lambda functions can also capture variables from the surrounding scope by including
them in the capture list. Here is an example of a lambda function that captures a
variable from the surrounding scope:
Copy code
int x = 5;
auto add_x = [x](int y) -> int { return x + y; };
std::cout << add_x(3) << std::endl; // prints 8
In this example, the lambda function add_x captures the variable x from the
surrounding scope and uses it to add it to the input parameter y and returns the
result.
Lambda functions are useful for creating small, one-time-use functions, and they
can be passed as arguments to other functions, making it easy to define and use
small, specialized functions when needed. They are often used in conjunction with
the STL algorithms and the functional programming features of C++.
example
Sure, here's a simple example that demonstrates the use of a lambda function:
Copy code
#include <iostream>
int main()
{
// Define a lambda function that takes two integers as input and returns their sum
auto add = [](int x, int y) -> int { return x + y; };
std::cout << add(3, 4) << std::endl; // prints 7
// Define a lambda function that captures a variable from the surrounding scope
int x = 5;
auto add_x = [x](int y) -> int { return x + y; };
std::cout << add_x(3) << std::endl; // prints 8
return 0;
}
In this example, the first lambda function add takes two integers as input and
returns their sum. The second lambda function add_x captures the variable x from
the surrounding scope and uses it to add it to the input parameter y and returns
the result. The type of the lambda function is inferred by the compiler from the
return type and the parameter types. The auto keyword is used to store the lambda
function in a variable.
In this way, the lambda functions are small, one-time-use functions that are
defined and used on the spot, making them useful for creating small, one-time-use
functions and they can be passed as arguments to other functions, making it easy
to define and use small, specialized functions when needed.
Lambda functions and regular functions both allow you to define and execute a block of code, but there are some key differences between them:
Syntax: Lambda functions have a more concise syntax than regular functions, as they don't require a function name or a separate function declaration.
Scope: Lambda functions are defined in the scope where they are used, whereas regular functions are defined in the global scope or in a namespace. This allows lambda functions to capture variables from the surrounding scope, which is not possible with regular functions.
Flexibility: Lambda functions can be defined and used on the spot, making them useful for creating small, one-time-use functions. Regular functions, on the other hand, require a separate function declaration, which can make them less flexible.
Performance: Lambda functions might be less efficient than regular functions, as they are typically implemented as functors (objects that can be called like functions) which may introduce some overhead.
The main use case of lambda functions is when you need to define a small, one-time-use function that can be passed as an argument to another function or used in a functional programming context. For example, they can be used as a function argument in STL algorithms such as std::sort, std::for_each, std::transform, etc.
Here is an example of using a lambda function with std::sort:
Copy code
#include <algorithm>
#include <vector>
int main() {
std::vector<int> v{ 2, 4, 1, 3, 5 };
std::sort(v.begin(), v.end(), [](int a, int b) { return a < b; });
for (auto i : v) {
std::cout << i << " ";
}
return 0;
}
In this example, the lambda function [](int a, int b) { return a < b; } is used
as the third argument of the std::sort function. This lambda function compares
two integers and returns true if the first integer is less than the second, which
causes the std::sort function to sort the elements of the vector in ascending
order.
In general, regular functions are more suitable for defining reusable,
complex logic, and are better for structuring your program, while lambda functions
are useful for defining small, one-time-use functions, and are particularly useful
in conjunction with the functional programming features of C++.
To embed this project on your website, copy the following code and paste it into your website's HTML: