Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Lambda Expressions | Functional Programming
C++ Modern Features

bookLambda 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

main.cpp

copy
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.

question mark

Which statement about lambda expressions in C++ is true?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

Suggested prompts:

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

bookLambda Expressions

Stryg for at vise menuen

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

main.cpp

copy
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.

question mark

Which statement about lambda expressions in C++ is true?

Select the correct answer

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 1
some-alt