Labs ICT
🎓 SIWES/IT Login

Lambda Expressions

1 min read | C++ Tutorial
🎓

Doing SIWES / IT attachment?

Get a structured training programme, a supervised digital logbook, and a completion certificate with LabsICT SIWES / IT.

Explore SIWES / IT Training

C++ Lambda Expressions

Lambda expressions create anonymous function objects inline. The syntax is [capture](parameters){body}. The capture clause (e.g., [&] by reference, [=] by value, [x] specific variable) controls which outside variables are accessible. Lambdas are ideal for short callbacks, predicates for algorithms, and replacing functors.

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> nums = {1, 2, 3, 4, 5};
    int multiplier = 3;
    std::for_each(nums.begin(), nums.end(),
        [multiplier](int n) {
            std::cout << n * multiplier << " ";
        });
    return 0;
}