Lambda

A lambda function is an anonymous function that you can define inline within your code. It allows you to create a function object on the fly without explicitly defining a named function. Lambda functions are useful when you need a short, one-time function that doesn’t require a separate declaration.

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

[capture list] (parameter list) -> return_type {
    // Function body
}

Let’s break down the syntax components:

  • Capture list: It specifies which variables from the enclosing scope are accessible inside the lambda function. It can be empty [], capture individual variables [var1, var2], capture by reference [&var1, &var2], or capture by value [=], among other options. By default, variables are captured by value.
  • Parameter list: It lists the parameters that the lambda function takes. You can specify their types or use type inference.
  • Return type: It specifies the return type of the lambda function. If the function body is a single expression, the return type can be omitted, and it will be deduced automatically.
  • Function body: It contains the code that defines the behavior of the lambda function.

Note that lambda functions can also have mutable state by using the mutable keyword, allowing you to modify captured variables.

Lambda functions provide a concise way to define small functions inline, making your code more readable and maintainable.

1.Basic: Here is an example of a lambda function in C++:

#include <iostream>

int main() {
    int a = 5;
    int b = 10;

    // Lambda function that adds two integers and prints the result
    auto add = [](int x, int y) {
        int sum = x + y;
        std::cout << "Sum: " << sum << std::endl;
    };

    // Call the lambda function
    add(a, b);

    return 0;
}
  1. Lambda Function with Capture List: The capture list allows you to capture variables from the enclosing scope and use them inside the lambda function. Example:
#include <iostream>

int main() {
    int x = 5;
    int y = 10;

    // Creating a lambda function that captures x and y, and returns their product
    auto product = [x, y]() -> int {
        return x * y;
    };

    // Calling the lambda function
    int result = product();
    std::cout << "Product: " << result << std::endl;

    return 0;
}
  1. Lambda Function as a Function Parameter: Lambda functions can be passed as parameters to other functions, providing a convenient way to define small functions on the fly. Example:
#include <iostream>
#include <vector>
#include <algorithm>

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

    // Sorting the vector using a lambda function as the comparison criterion
    std::sort(numbers.begin(), numbers.end(), [](int a, int b) {
        return a < b;
    });

    // Printing the sorted vector
    for (int num : numbers) {
        std::cout << num << " ";
    }
    std::cout << std::endl;

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *