Tutorials Logic, IN info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Website Development
Practice
Quiz Challenge Interview Questions Certification Practice
Tools
Online Compiler JSON Formatter Regex Tester CSS Unit Converter Color Picker
Compiler Tools

C++ Lambda Expressions Captures STL Use: Tutorial, Examples, FAQs & Interview Tips

What is a Lambda?

A lambda expression (C++11) is an anonymous inline function - a function without a name, defined right where it's used. Lambdas are especially useful with STL algorithms.

Syntax: [capture](parameters) -> returnType { body }

CaptureMeaning
[]Capture nothing
[=]Capture all local variables by value
[&]Capture all local variables by reference
[x]Capture x by value
[&x]Capture x by reference
[=, &x]Capture all by value, x by reference
Lambda Basics and Captures
#include <iostream>
#include <functional>
using namespace std;

int main() {
    // Basic lambda - no capture, no parameters
    auto hello = []() { cout << "Hello from lambda!" << endl; };
    hello();

    // Lambda with parameters
    auto add = [](int a, int b) { return a + b; };
    cout << "3 + 4 = " << add(3, 4) << endl;

    // Lambda with explicit return type
    auto divide = [](double a, double b) -> double {
        if (b == 0) return 0;
        return a / b;
    };
    cout << "10 / 3 = " << divide(10, 3) << endl;

    // Capture by value
    int x = 10;
    auto addX = [x](int n) { return n + x; };  // x captured by value
    x = 99;  // changing x doesn't affect the lambda
    cout << addX(5) << endl;  // 15 (uses captured x=10)

    // Capture by reference
    int counter = 0;
    auto increment = [&counter]() { counter++; };
    increment(); increment(); increment();
    cout << "Counter: " << counter << endl;  // 3

    // Store lambda in std::function
    function<int(int, int)> multiply = [](int a, int b) { return a * b; };
    cout << "5 * 6 = " << multiply(5, 6) << endl;

    return 0;
}

Lambdas with STL Algorithms

Lambdas with sort, find_if, for_each
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

    // sort descending with lambda comparator
    sort(nums.begin(), nums.end(), [](int a, int b) { return a > b; });
    for (int n : nums) cout << n << " ";  // 9 8 5 3 2 1
    cout << endl;

    // find_if - first element > 4
    auto it = find_if(nums.begin(), nums.end(), [](int n) { return n > 4; });
    if (it != nums.end()) cout << "First > 4: " << *it << endl;  // 9

    // for_each - print each element
    for_each(nums.begin(), nums.end(), [](int n) { cout << n * 2 << " "; });
    cout << endl;  // 18 16 10 6 4 2

    // count_if - count even numbers
    int evens = count_if(nums.begin(), nums.end(), [](int n) { return n % 2 == 0; });
    cout << "Even count: " << evens << endl;  // 2

    // remove_if - remove elements < 4
    nums.erase(remove_if(nums.begin(), nums.end(), [](int n){ return n < 4; }), nums.end());
    for (int n : nums) cout << n << " ";  // 9 8 5
    cout << endl;

    return 0;
}

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.