Lambda Expression in C++

Last Updated : 30 Jan, 2026

Lambda expressions in C++ are anonymous, inline functions introduced in C++11 that allow writing small pieces of logic directly at the place of use. They improve code readability by keeping behavior close to where it is applied and remove the need for separate named functions.

  • Used to ass custom logic to algorithms like sort, for_each, and find_if in C++ STL.
  • Used in Callbacks and Event Handling for asynchronous tasks and events.
C++
#include <iostream>
using namespace std;

int main() {
    
    // Defining a lambda
    auto res = [](int x) {
        return x + x;
    };
    
    // Using the lambda
    cout << res(5);
    
    return 0;
}

Output
10

Explanation: The lambda expression in the above program takes an integer x as input and returns the sum of x with itself.

Syntax

lambda_expression_in_c_
Syntax of lambda Function
  • Parameters: These parameters are similar to the function parameters in every way.
  • Return Type: Generally, the return-type in lambda expressions is evaluated by the compiler itself and we don’t need to specify it explicitly. However, in some complex cases e.g. conditional statements, the compiler can’t determine the return type and explicit specification is required.

Capture Clause

A lambda expression can have more power than an ordinary function by having access to variables from the enclosing scope. We can capture external variables from the enclosing scope in three ways using capture clause:

  • [&]: capture all external variables by reference.
  • [=]: capture all external variables by value.
  • [a, &b]: capture 'a' by value and 'b' by reference.

Note: A lambda with an empty capture clause [] can only access variables which are local to it.

C++
#include <iostream>
#include <vector>
using namespace std;

void print(vector<int> v)
{
    for (auto x : v)
        cout << x << " ";
    cout << endl;
}

int main()
{
    vector<int> v1, v2;

    //  Capture all by reference
    auto byRef = [&](int m) {
        v1.push_back(m);
        v2.push_back(m);
    };

    //  Capture all by value
    auto byVal = [=](int m) mutable {
        v1.push_back(m);
        v2.push_back(m);
    };

    //  Capture v1 by reference and v2 by value
    auto mixed = [&v1, v2](int m) mutable {
        v1.push_back(m);
        v2.push_back(m);
    };

    // Case 1: byRef — modifies both v1 and v2
    byRef(20);

    // Case 2: byVal — modifies only copies (originals unchanged)
    byVal(234);

    // Case 3: mixed — modifies only v1 (since v2 is captured by value)
    mixed(10);

    print(v1);
    print(v2);

    return 0;
}

Output
20 10 
20 

Explanation:

  • byRef captures all by reference. So pushing 20 will push it into original v1 and v2.
  • byVal captures all by value. So pushing 234 will not do anything to original vectors.
  • mixed captures v1 by reference and v2 by value. So pushing 10 will only push it into v1.

The mutable keyword here is used in capture by value lambdas only because, by default, value captured objects are const.

Lambda Expressions with STL:

1. Sort Vector in Descending Order

C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	vector<int> v = {5, 1, 8, 3, 9, 2};

	// Sort in descending order
	sort(v.begin(), v.end(), [] (const int& a, const int&b) {
		return a > b;
	});

	for (int x : v)
		cout << x << " ";
	return 0;
}

Output
9 8 5 3 2 1 

Explanation:

  • The program stores numbers in a vector and uses sort() to arrange them.
  • A lambda function "[](const int& a, const int& b){ return a > b; }" is passed to sort() to compare elements and sort them in descending order.
  • Finally, the sorted values are printed using a range-based for loop.

2. Find First Number Divisible by 3

CPP
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
	vector<int> v = {5, 1, 8, 3, 9, 2};

    // Find first element divisible by 3
	auto it = find_if(v.begin(), v.end(), [] (const int& a) {
		return a % 3 == 0;
	});

    if (it != v.end()) cout << *it;
	else cout << "No such element";
	return 0;
}

Output
3

Explanation:

  • The program stores integers in a vector and searches for a specific value using find_if().
  • A lambda function "[](const int& a){ return a % 3 == 0; }" is used as a condition to find the first element divisible by 3.
  • If such an element is found, it is printed, otherwise, the program prints "No such element".
Comment