Lambda Expressions in c++

avatar
(Edited)

Mithilfe von lambda expressions (Lambda-Ausdrücke) lassen sich normale Funktionen einfacher und platzsparender Implementieren.
In einem Lambda-Ausdruck kann man, wie auch bei Funktionen, Variablen übergeben. Man kann außerdem auch auf Variablen aus dem umgebenden Bereich (captures) zugreifen. Also auf Variablen die innerhalb des Gültigkeitsbereiches und oberhalb des Lambda-Ausdrucks definiert sind.
Man kann ein Lambda-Ausdruck direkt nach seiner Definition verwenden oder aber als Parameter an eine weitere Funktion übergeben.

Die allgemeine Syntax sieht folgendermaßen aus:

auto lambda = [capture](paramlist) -> returntype {codeblock};

Die Parameterliste und das Capturing sind optional. Den Rückgabetyp anzugeben ist auch optional.

Hier folgen nun noch ein paar kleine Beispiele:

/** normal function **/
int withoutLambda(int x, int y){
    return x + y;
}

int main() {
    /* example without lambda expression, normal function call */
    std::cout << withoutLambda(3,7) << "\n";

    /* Define lambda expression here to use it later */
    auto lambda = [](int x, int y){return x + y;};

    /* use the above lambda here with arguments */
    int result = lambda(2,3);
    std::cout << result << "\n";
    return 0;
}

1. Difference between a function and lambda expression / Unterschied zwischen einer Funktion und einem Lambda-Ausdruck

int main() {

    /* example of how to use captures (square brackets) with lambda expressions */
    int param = 5;
    int multi = 2;

    /* You can use captures if you want to use arguments
     * which were defined before the lambda expression */
    auto lambda = [&,param](int x){return (x * multi) + param;};
    
    /* [&,param] means: Pass the param variable as a copy but all other
     * variables by referrence (&). You can use (=) to pass variables
     * as constantes */

    int result = lambda(2);
    std::cout << result << "\n";
    return 0;
}

2. Example of how to use captures / Beispiel wie man die Umgebung einfangen kann

/* required to pass a lambda expression 'easier' */
#include <functional>

void printResult(int x, int y, std::function<int(int,int)>lambda){
    std::cout << lambda(x,y) << "\n";
}

int main() {

    /* you can also use lambda expressions as function arguments
     * and execute the lambda inside another function */
    auto lambda = [](int x, int y){return x + y;};
    int param1 = 3;
    int param2 = 7;

    /* function with two parameters and lambda as arguments */
    printResult(param1,param2,lambda);
    return 0;
}

3. Example of how to pass a lambda expression / Beispiel wie man ein Lambda-Ausdruck als Parameter übergeben kann



0
0
0.000
1 comments
avatar

Congratulations @ozelot47! You have completed the following achievement on the Hive blockchain and have been rewarded with new badge(s) :

You have been a buzzy bee and published a post every day of the week

You can view your badges on your board and compare yourself to others in the Ranking
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Check out the last post from @hivebuzz:

Feedback from the March 1st Hive Power Up Day
0
0
0.000