Lambda Functions in C++
Introduction
Lambda functions, also known as lambda expressions, were introduced in C++11. They are a convenient way to define anonymous functions directly in the place where they are needed. Lambda functions can capture variables from their surrounding scope and can be used to simplify code, especially when dealing with algorithms and functional programming.
Syntax of Lambda Functions
The general syntax of a lambda function is as follows:
[capture](parameters) -> return_type { body }
Here,
- capture: Specifies which variables from the surrounding scope are to be used inside the lambda.
- parameters: The parameter list for the lambda function (similar to a regular function).
- return_type: The return type of the lambda function. This can often be omitted as the compiler can deduce it.
- body: The function body where the actual operations are performed.
Basic Examples
Let's start with a simple example to demonstrate a basic lambda function:
#include <iostream> int main() { auto add = [](int a, int b) { return a + b; }; std::cout << "Sum: " << add(3, 4) << std::endl; return 0; }
In this example, we define a lambda function add
that takes two integers and returns their sum. The output of this program will be:
Capturing Variables
Lambda functions can capture variables from their enclosing scope. There are several ways to capture variables:
- By value: Specified using the
=
symbol. - By reference: Specified using the
&
symbol. - Explicitly: By listing the variables inside the square brackets.
Here's an example demonstrating variable capture:
#include <iostream> int main() { int x = 10; int y = 20; auto add = [x, y]() { return x + y; }; std::cout << "Sum: " << add() << std::endl; return 0; }
In this example, the lambda function captures the variables x
and y
by value. The output will be:
Mutable Lambdas
By default, lambda functions that capture variables by value are not allowed to modify those variables. However, you can make a lambda function mutable by using the mutable
keyword.
#include <iostream> int main() { int x = 10; auto increment = [x]() mutable { x++; return x; }; std::cout << "Incremented value: " << increment() << std::endl; std::cout << "Original value: " << x << std::endl; return 0; }
In this example, the lambda function captures the variable x
by value and modifies it. The output will be:
Original value: 10
Returning Values
Lambda functions can also return values. The return type can either be explicitly specified or deduced by the compiler. Here's an example:
#include <iostream> int main() { auto square = [](int n) -> int { return n * n; }; std::cout << "Square: " << square(5) << std::endl; return 0; }
In this example, the lambda function square
returns the square of the input integer. The output will be:
Lambda with Algorithms
Lambda functions are particularly useful when working with algorithms. For example, you can use them with the std::sort
function to define custom sorting criteria:
#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> v = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5}; std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; }); std::cout << "Sorted vector: "; for (int n : v) { std::cout << n << " "; } std::cout << std::endl; return 0; }
In this example, the lambda function is used to sort the vector in descending order. The output will be:
Conclusion
Lambda functions in C++ provide a powerful and concise way to define anonymous functions on the fly. They are especially useful for short-lived operations and when working with algorithms. Understanding how to use lambda functions effectively can greatly enhance your C++ programming skills and make your code more readable and maintainable.