A lambdaexpression (also known as an anonymous function) in C++ is a short function that is defined inline. It is typically used to pass a function as an argument to another function or to create a closure.
Lambda expressions are defined using the lambda keyword. The syntax for a lambda expression is as follows:
C++
[capture] (parameters) mutable -> return_type { body }
capture: The capture list, which specifies the variables that are captured from the surrounding scope.
parameters: The parameter list, which specifies the arguments to the function.
mutable: The mutable keyword, which specifies that the captured variables can be modified by the lambda expression.
return_type: The return type of the function.
body: The body of the function, which is the code that is executed when the function is called.
The following is an example of a lambda expression that adds two numbers:
C++
auto add_two_numbers = [](int x, int y) { return x + y; };
In this code, the add_two_numbers lambda expression takes two numbers as its arguments and returns the sum of the two numbers. The
auto keyword is used to infer the return type of the function.
Lambda expressions can be used in a variety of places in C++, including:
As the argument to a function: Lambda expressions can be used as the argument to a function. For example, the following code uses a lambda expression as the argument to the
std::sort() function to sort a list of numbers in ascending order:
C++
std::vector<int> numbers = { 10, 5, 20 };
std::sort(numbers.begin(), numbers.end(), [](int x, int y) { return x < y; });
As a value: Lambda expressions can be used as a value. For example, the following code defines a variable that stores a lambda expression that adds two numbers:
C++
auto add_two_numbers = [](int x, int y) { return x + y; };
int sum = add_two_numbers(10, 20);
In comprehensions: Lambda expressions can be used in comprehensions. For example, the following code creates a list of the squares of the numbers from 1 to 10:
C++
std::vector<int> squares = { x * x for x in range(1, 11) };
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
A lambda expression (also known as an anonymous function) in C++ is a short function that is defined inline. It is typically used to pass a function as an argument to another function or to create a closure.
Lambda expressions are defined using the
lambdakeyword. The syntax for a lambda expression is as follows:C++
capture: The capture list, which specifies the variables that are captured from the surrounding scope.parameters: The parameter list, which specifies the arguments to the function.mutable: Themutablekeyword, which specifies that the captured variables can be modified by the lambda expression.return_type: The return type of the function.body: The body of the function, which is the code that is executed when the function is called.The following is an example of a lambda expression that adds two numbers:
C++
In this code, the
add_two_numberslambda expression takes two numbers as its arguments and returns the sum of the two numbers. Theautokeyword is used to infer the return type of the function.Lambda expressions can be used in a variety of places in C++, including:
std::sort()function to sort a list of numbers in ascending order:C++
C++
C++