Lambda Expressions
Lambda expressions in modern C++ allow you to define anonymous functions directly in your code. They are especially useful for short, inline operations such as sorting, filtering, or customizing algorithms. The basic syntax of a lambda is [capture](parameters) { body }. The capture clause lets you specify which variables from the surrounding scope the lambda can access. You can capture variables by value using = or by reference using &. For example, [=] captures all needed variables by value, while [&] captures all by reference. You can also specify individual variables, such as [x, &y]. Lambdas can be assigned to variables, passed to functions, or used immediately where defined.
main.cpp
1234567891011121314151617181920212223242526#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5, 6}; int count = 0; // Lambda to filter even numbers, capturing 'count' by reference and 'numbers' by value auto print_even = [&, numbers]() mutable { std::cout << "Even numbers: "; for (int n : numbers) { if (n % 2 == 0) { std::cout << n << " "; ++count; // count is captured by reference } } std::cout << "\n"; }; print_even(); std::cout << "Total even numbers: " << count << "\n"; }
Lambdas are ideal when you want concise, readable code for operations used only once or in a limited scope, such as passing custom predicates to algorithms. However, you should be careful with captures. Accidentally capturing variables by reference can lead to bugs if the referenced variable goes out of scope, while capturing by value may not reflect changes to the original variable. Always choose the capture mode that matches your intent.
¡Gracias por tus comentarios!
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Can you give some examples of lambda expressions in C++?
How do I decide when to use value vs reference capture in lambdas?
What are some common mistakes to avoid when using lambdas?
Awesome!
Completion rate improved to 14.29
Lambda Expressions
Desliza para mostrar el menú
Lambda expressions in modern C++ allow you to define anonymous functions directly in your code. They are especially useful for short, inline operations such as sorting, filtering, or customizing algorithms. The basic syntax of a lambda is [capture](parameters) { body }. The capture clause lets you specify which variables from the surrounding scope the lambda can access. You can capture variables by value using = or by reference using &. For example, [=] captures all needed variables by value, while [&] captures all by reference. You can also specify individual variables, such as [x, &y]. Lambdas can be assigned to variables, passed to functions, or used immediately where defined.
main.cpp
1234567891011121314151617181920212223242526#include <iostream> #include <vector> #include <algorithm> int main() { std::vector<int> numbers{1, 2, 3, 4, 5, 6}; int count = 0; // Lambda to filter even numbers, capturing 'count' by reference and 'numbers' by value auto print_even = [&, numbers]() mutable { std::cout << "Even numbers: "; for (int n : numbers) { if (n % 2 == 0) { std::cout << n << " "; ++count; // count is captured by reference } } std::cout << "\n"; }; print_even(); std::cout << "Total even numbers: " << count << "\n"; }
Lambdas are ideal when you want concise, readable code for operations used only once or in a limited scope, such as passing custom predicates to algorithms. However, you should be careful with captures. Accidentally capturing variables by reference can lead to bugs if the referenced variable goes out of scope, while capturing by value may not reflect changes to the original variable. Always choose the capture mode that matches your intent.
¡Gracias por tus comentarios!