🔴 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.
Level
🔴 STL, Modern C++ and Projects
Example File
lambda.cpp
Main Focus
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

TermMeaning / Use
lambdaLambda used in Lambda Expressions programming.
captureCapture used in Lambda Expressions programming.
anonymous functionAnonymous function used in Lambda Expressions programming.
predicatePredicate 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

  1. Use lambda to count marks above 80.
  2. 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.
Level
🔴 STL, Modern C++ and Projects
Example File
lambda.cpp
Main Focus
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

TermMeaning / Use
lambdaLambda used in Lambda Expressions programming.
captureCapture used in Lambda Expressions programming.
anonymous functionAnonymous function used in Lambda Expressions programming.
predicatePredicate 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

  1. Use lambda to count marks above 80.
  2. 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.

← Back to C++ Tutorial