🔴 Advanced · Lesson 60
Lambda Expressions
Lambda Expressions
What is Lambda Expressions?
Lambda Expressions
A lambda expression is an unnamed function written directly where it is needed. It is commonly used with STL algorithms.
A lambda expression is an unnamed function written directly where it is needed. It is commonly used with STL algorithms.
Level
🔴 STL, Modern C++ and Projects
🔴 STL, Modern C++ and Projects
Example File
lambda.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| lambda | Lambda used in Lambda Expressions programming. |
| capture | Capture used in Lambda Expressions programming. |
| anonymous function | Anonymous function used in Lambda Expressions programming. |
| predicate | Predicate used in Lambda Expressions programming. |
Syntax / Pattern
[capture](parameters){ body }
Example Program
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> v = {1,2,3,4};
int even = count_if(v.begin(), v.end(), [](int x){ return x%2==0; });
cout << even;
}
Expected Output
2
Program Explanation
- Lambda checks whether a number is even.
- count_if counts elements satisfying the condition.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- custom sorting
- filtering
- callbacks
Common Mistakes
- Writing unreadable long lambdas.
- Forgetting capture when using outside variables.
Practice Tasks
- Use lambda to count marks above 80.
- Sort strings by length.
Summary
Lambda Expressions is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.
What is Lambda Expressions?
Lambda Expressions
A lambda expression is an unnamed function written directly where it is needed. It is commonly used with STL algorithms.
A lambda expression is an unnamed function written directly where it is needed. It is commonly used with STL algorithms.
Level
🔴 STL, Modern C++ and Projects
🔴 STL, Modern C++ and Projects
Example File
lambda.cppMain Focus
Concept + syntax + practical C++ program
Concept + syntax + practical C++ program
Why should you learn this?
- It helps you write correct and readable C++ programs.
- It is used repeatedly in school practicals, projects and competitive programming.
- It builds the base for advanced topics such as OOP, STL and data structures.
Important Terms
| Term | Meaning / Use |
|---|---|
| lambda | Lambda used in Lambda Expressions programming. |
| capture | Capture used in Lambda Expressions programming. |
| anonymous function | Anonymous function used in Lambda Expressions programming. |
| predicate | Predicate used in Lambda Expressions programming. |
Syntax / Pattern
[capture](parameters){ body }
Example Program
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(){
vector<int> v = {1,2,3,4};
int even = count_if(v.begin(), v.end(), [](int x){ return x%2==0; });
cout << even;
}
Expected Output
2
Program Explanation
- Lambda checks whether a number is even.
- count_if counts elements satisfying the condition.
Exam Tip: In C++ practical answers, write the logic first, then the program, then expected output. For theory, always include one suitable example.
Where will you use it?
- custom sorting
- filtering
- callbacks
Common Mistakes
- Writing unreadable long lambdas.
- Forgetting capture when using outside variables.
Practice Tasks
- Use lambda to count marks above 80.
- Sort strings by length.
Summary
Lambda Expressions is an important C++ topic. Learn the definition, understand the syntax, run the example program and then solve the practice tasks to make the concept strong.