Language: EN

cpp-funciones-lambda

What are Lambda Functions in C++

In C++ a lambda function is a compact syntax for defining anonymous functions, without the need to declare it explicitly as a traditional function.

These functions are especially useful for simple and quick operations. For example, when it comes to operations on collections, such as filtering, mapping, or reducing.

If you want to learn more about Lambda Functions
check the Introduction to Programming Course read more ⯈

Syntax of Lambda Functions

The basic syntax of a lambda function in C++ is as follows:

[capture](parameters) -> return_type { body }
  • Capture: List of environment variables that the lambda can use.
  • Parameters: List of input parameters, separated by commas if there is more than one.
  • Return Type: Type of value returned by the lambda, optional if the type can be deduced.
  • Body: The code that executes when the lambda is invoked.

Basic Example

Here is a basic example of a lambda function that adds two numbers:

#include <iostream>

int main() {
    auto add = [](int x, int y) -> int {
        return x + y;
    };

    std::cout << "Sum: " << add(5, 3) << std::endl; // Output: 8

    return 0;
}

In this example,

  • The lambda function add takes two parameters x and y, and returns their sum.
  • The keyword auto is used to deduce the type of the lambda, and -> int specifies the return type.

Variable Capture

Lambdas can capture variables from the context in which they are defined. This is done through the capture list [capture].

Capture by value:

#include <iostream>

int main() {
    int value = 10;

    auto lambda = [value]() {
        std::cout << "Value: " << value << std::endl;
    };

    lambda(); // Output: Value: 10

    return 0;
}

Capture by reference:

#include <iostream>

int main() {
    int value = 10;

    auto lambda = [&value]() {
        value++;
        std::cout << "Value: " << value << std::endl;
    };

    lambda(); // Output: Value: 11
    std::cout << "Value after lambda: " << value << std::endl; // Output: Value after lambda: 11

    return 0;
}

Use in Higher-Order Functions

Lambda functions can be passed as arguments to other functions that accept functions or functional objects.

Example with std::function:

#include <iostream>
#include <functional>

void Execute(std::function<void()> func) {
    func();
}

int main() {
    auto message = []() {
        std::cout << "Hello from a lambda function" << std::endl;
    };

    Execute(message);

    return 0;
}

Practical Examples

Use in STL Algorithms

Lambda functions are widely used with STL algorithms, such as std::sort, std::for_each, and std::find_if.

Sorting a collection:

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

int main() {
    std::vector<int> numbers = {5, 2, 9, 1, 5, 6};

    // Sort using a lambda function
    std::sort(numbers.begin(), numbers.end(), [](int a, int b) {
        return a < b;
    });

    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Filtering a collection:

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

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5, 6};

    // Filter even numbers
    std::vector<int> evens;
    std::copy_if(numbers.begin(), numbers.end(), std::back_inserter(evens), [](int num) {
        return num % 2 == 0;
    });

    for (int num : evens) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Use in std::for_each

The lambda function is very useful with std::for_each to apply an action to each element of a collection.

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

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};

    // Apply a lambda function to each element
    std::for_each(numbers.begin(), numbers.end(), [](int num) {
        std::cout << num << " ";
    });
    std::cout << std::endl;

    return 0;
}