create account

Lambda Expressions in c++ by ozelot47

View this thread on: hive.blogpeakd.comecency.com
· @ozelot47 ·
$3.02
Lambda Expressions in c++
In diesem Beitrag zeige ich ein kleines Beispiel, wie man die <b>Lambda Expressions</b> in c++ nutzen kann. Lambdas werden in der funktionalen Programmierung eingesetzt, können aber auch in anderen Programmierparadigmen verwendet werden. Dadurch kann man zum Beispiel eine Funktion als Parameter an eine anderen Funktion übergeben. Man kann Lambdas auch als eine "kurzschreibweise" einer normalen Funktionsdefinition sehen.
```
#include <iostream>
#include <vector>

auto print = []<typename T>(T result) -> void {std::cout << result << '\n';};
auto insertNumbers = []<typename T>(std::vector<T> *list, T value) -> void {list->push_back(value);};
auto sumUp = []<typename T>(const std::vector<T> *list) -> T {
    T sum = 0;
    for(T elem : *list){
        sum += elem;
    }
    return sum;
};

int main(){
    std::vector<int> numbers = std::vector<int>();
    for(int n = 0; n < 10; n++){
        insertNumbers(&numbers, n+1);
    }
    int sum = sumUp(&numbers);
    print(sum);
}

```

https://img.blurt.world/blurtimage/ozelot47/cf187aa98ca3e8ae7f187d67d61c14ac1c778de9.png
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorozelot47
permlinklambda-expressions-in-c-s16gqc
categoryhive-121566
json_metadata{"app":"peakd/2023.9.1","format":"markdown","tags":["stemgeeks","science","stem","deutsch","hive-engine","archon","dach","lambda"],"users":[],"image":["https://img.blurt.world/blurtimage/ozelot47/cf187aa98ca3e8ae7f187d67d61c14ac1c778de9.png"]}
created2023-09-18 10:49:24
last_update2023-09-18 10:49:24
depth0
children6
last_payout2023-09-25 10:49:24
cashout_time1969-12-31 23:59:59
total_payout_value1.515 HBD
curator_payout_value1.509 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,081
author_reputation142,895,102,111,771
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,258,219
net_rshares7,370,240,084,263
author_curate_reward""
vote details (47)
@dotwin1981 ·
!LOL
!invest_vote
!BEER
properties (22)
authordotwin1981
permlinkre-ozelot47-s174wt
categoryhive-121566
json_metadata{"tags":["hive-121566"],"app":"peakd/2023.9.1"}
created2023-09-18 19:31:42
last_update2023-09-18 19:31:42
depth1
children1
last_payout2023-09-25 19:31:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length23
author_reputation333,113,166,594,230
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,271,250
net_rshares0
@lolzbot ·
<div class='pull-right'><center><img src="https://lolztoken.com/lolz.png"><br><a href="https://lolztoken.com">lolztoken.com</a></p><br><br><br><br></center></div><p><center><strong>Never scream into a colander<br>You'll strain your voice.</strong><br><sub>Credit: <a href="https://peakd.com/@reddit">reddit</a></sub><br>@ozelot47, I sent you an <a href="https://lolztoken.com">$LOLZ</a> on behalf of dotwin1981<br><br>(8/10)<br>Farm <strong><a href='https://lolztoken.com'>LOLZ tokens</a></strong> when you <strong><a href='https://peakd.com/hive-155986/@lolztoken/earn-10percent-apr-on-hive-power-delegations-to-the-lolz-project'>Delegate Hive</a> or <a href='https://peakd.com/hive-155986/@lolztoken/introducing-lolz-defi-now-you'>Hive Tokens</a>.</strong><br>Click to delegate: <a href='https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=lolzbot&vesting_shares=10%20HP'>10</a> - <a href='https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=lolzbot&vesting_shares=20%20HP'>20</a> - <a href='https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=lolzbot&vesting_shares=50%20HP'>50</a> - <a href='https://hivesigner.com/sign/delegateVestingShares?delegator=&delegatee=lolzbot&vesting_shares=100%20HP'>100</a> HP</center></p>
properties (22)
authorlolzbot
permlinkre-re-ozelot47-s174wt-20230918t193208z
categoryhive-121566
json_metadata"{"app": "beem/0.24.19"}"
created2023-09-18 19:32:15
last_update2023-09-18 19:32:15
depth2
children0
last_payout2023-09-25 19:32:15
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,280
author_reputation196,320,608,704,749
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,271,260
net_rshares0
@investinthefutur ·
@dotwin1981 denkt du hast ein Vote durch @investinthefutur verdient!<br>@dotwin1981 thinks you have earned a vote of @investinthefutur !<br>
properties (22)
authorinvestinthefutur
permlinkre-lambda-expressions-in-c-s16gqc-20230918t193200z
categoryhive-121566
json_metadata"{"app": "beem/0.24.26"}"
created2023-09-18 19:32:00
last_update2023-09-18 19:32:00
depth1
children0
last_payout2023-09-25 19:32:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length140
author_reputation135,729,607,290,395
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,271,256
net_rshares0
@jeyf123 ·
Ich kenne Lambdas momentan auch eher als Kurzschreibweise von Funktionen in Python.
Dort bin ich aktuell grad beim Regex Thema.
👍  
properties (23)
authorjeyf123
permlinkre-ozelot47-s16jk1
categoryhive-121566
json_metadata{"tags":["hive-121566"],"app":"peakd/2023.9.1"}
created2023-09-18 11:50:27
last_update2023-09-18 11:50:27
depth1
children2
last_payout2023-09-25 11:50:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length127
author_reputation85,991,437,710,893
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,259,235
net_rshares42,778,995,080
author_curate_reward""
vote details (1)
@ozelot47 ·
Mit regulären Ausdrücken kann man schön spielen. Ist ein mächtiges Werkzeug.
properties (22)
authorozelot47
permlinkre-jeyf123-s16nr0
categoryhive-121566
json_metadata{"tags":["hive-121566"],"app":"peakd/2023.9.1"}
created2023-09-18 13:21:00
last_update2023-09-18 13:21:00
depth2
children1
last_payout2023-09-25 13:21:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length76
author_reputation142,895,102,111,771
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,261,292
net_rshares0
@jeyf123 ·
ja, da hast du recht. Derzeit muss ich mir aber erst mal die Basis aneignen.
Manchmal unterscheiden sich die Angaben von der Realität. 


![Screenshot_20230918_165417.png](https://files.peakd.com/file/peakd-hive/jeyf123/23wXJB3HWXbydW8zRGea1EwAAxhNixry6c4BpTrR5H9zDhLM6hCwnjgqbjVyPqBbZxBBu.png)

Dann erzählt mir der Chatbot, der + Quantifier(heißt das so?) ist greedy/gierig und sucht die Buchstabenreihenfolge hintennach ("o") bis zum Ende des Strings. Interessanter Effekt, wenn man einen längeren Text einlesen lässt. (entsprechend bis zum letzten o)
properties (22)
authorjeyf123
permlinkre-ozelot47-s16s6a
categoryhive-121566
json_metadata{"tags":["hive-121566"],"app":"peakd/2023.9.1"}
created2023-09-18 14:56:42
last_update2023-09-18 14:56:42
depth3
children0
last_payout2023-09-25 14:56:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length555
author_reputation85,991,437,710,893
root_title"Lambda Expressions in c++"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,263,970
net_rshares0