Step 1: Basic Syntax
Example 1: Basic Lambda
A lambda is an anonymous function. Here’s how to define and call a basic lambda:
cpp
Copy code
#include <iostream>
int main() {
auto lambda = []() {
std::cout << "Hello, World!" << std::endl;
};
lambda(); // Call the lambda
return 0;
}
auto lambda = []() { /* code */ };: This defines a lambda.
lambda();: This calls the lambda.
Step 2: Capture List
Example 2: Capturing by Value
Capture variables from the surrounding scope by value:
cpp
Copy code
#include <iostream>
int main() {
int x = 10;
auto lambda = [x]() {
std::cout << "Captured value: " << x << std::endl;
};
lambda(); // Outputs: Captured value: 10
return 0;
}
[x]: Captures the variable x by value.
Example 3: Capturing by Reference
Capture variables by reference to modify them inside the lambda:
cpp
Copy code
#include <iostream>
int main() {
int x = 10;
auto lambda = [&x]() {
x += 5;
std::cout << "Modified value: " << x << std::endl;
};
lambda(); // Outputs: Modified value: 15
std::cout << "Value outside: " << x << std::endl; // Outputs: Value outside: 15
return 0;
}
[&x]: Captures the variable x by reference, allowing modifications.
Step 3: Lambda Parameters
Example 4: Lambda with Parameters
Lambdas can take parameters just like regular functions:
cpp
Copy code
#include <iostream>
int main() {
auto lambda = [](int a, int b) {
return a + b;
};
int result = lambda(3, 4); // Call the lambda with arguments
std::cout << "Result: " << result << std::endl; // Outputs: Result: 7
return 0;
}
[](int a, int b) { return a + b; }: Lambda that takes two parameters and returns
their sum.
Step 4: Return Types
Example 5: Specifying Return Type
You can specify the return type explicitly:
cpp
Copy code
#include <iostream>
int main() {
auto lambda = [](int a, int b) -> double {
return a / static_cast<double>(b);
};
double result = lambda(5, 2); // Call the lambda
std::cout << "Result: " << result << std::endl; // Outputs: Result: 2.5
return 0;
}
-> double: Specifies that the return type of the lambda is double.
Step 5: Mutable Lambdas
Example 6: Mutable Lambda
By default, lambdas that capture variables by value cannot modify those variables.
Use mutable to allow modification:
cpp
Copy code
#include <iostream>
int main() {
int x = 10;
auto lambda = [x]() mutable {
x += 5;
std::cout << "Inside lambda: " << x << std::endl;
};
lambda(); // Outputs: Inside lambda: 15
std::cout << "Outside lambda: " << x << std::endl; // Outputs: Outside lambda:
10
return 0;
}
mutable: Allows the lambda to modify its captured variables by value.
Step 6: Using Lambdas with STL Algorithms
Example 7: Using with std::for_each
Lambdas are often used with STL algorithms like std::for_each:
cpp
Copy code
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vec = {1, 2, 3, 4, 5};
std::for_each(vec.begin(), vec.end(), [](int &n) {
n *= 2;
});
for (int n : vec) {
std::cout << n << " "; // Outputs: 2 4 6 8 10
}
return 0;
}
std::for_each(vec.begin(), vec.end(), [](int &n) { n *= 2; });: Multiplies each
element in the vector by 2 using a lambda.