create account

Filter in c++ mit Lambda nutzen by ozelot47

View this thread on: hive.blogpeakd.comecency.com
· @ozelot47 ·
$5.17
Filter in c++ mit Lambda nutzen
Filter werden benutzt, um Operationen auf einer großen Datenmenge auszuführen. In diesem Beispiel ist die Datenmenge allerdings überschaubar. Zunächst wird jedes Element modifiziert (<b>map</b>), dann ein Filter angewendet, um die Menge zu verkleinern. Abschließend folgt eine Reduzierung, bis nur noch ein Element übrig bleibt. Ein Anwendungsfall wären Streams.

```
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

auto map = [](int &elem) -> int {return elem-1;};
auto filter = [](int &elem) -> bool {return elem % 2 == 0 || elem <= 0;};
auto reduce = [](int &x, int &y) -> int {return x*y;};

int main(){
    std::vector<int> origin{-3,-2,-1,0,1,2,3,4,5,6,7,8,9};
    std::vector<int> afterMap;

    // map: do something with all elements in this datastructure
    std::transform(origin.begin(), origin.end(), std::back_inserter(afterMap), map);

    // filter: conditions to skip elements
    std::vector<int>::iterator vectorfilter = std::remove_if(afterMap.begin(), afterMap.end(), filter);

    for(auto it = afterMap.begin(); it != vectorfilter; it++){
        std::cout << *it << '\n';
    };

    // reduce: to reduce elements in this container via sum, multiply or other things
    int result = std::accumulate(afterMap.begin(), vectorfilter, afterMap[0], reduce);
    std::cout << result << '\n';
}
```

![cpp_filter.png](https://img.blurt.world/blurtimage/ozelot47/aae19ceb71509e85109be2dd0bcc59fc34b7f10c.png)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorozelot47
permlinkfilter-in-c-mit-lambda-nutzen
categoryhive-121566
json_metadata{"app":"peakd/2023.9.2","format":"markdown","tags":["stemgeeks","science","stem","deutsch","hive-engine","leofinance","archon"],"users":[],"image":["https://img.blurt.world/blurtimage/ozelot47/aae19ceb71509e85109be2dd0bcc59fc34b7f10c.png"]}
created2023-09-25 10:30:03
last_update2023-09-25 10:30:03
depth0
children4
last_payout2023-10-02 10:30:03
cashout_time1969-12-31 23:59:59
total_payout_value2.590 HBD
curator_payout_value2.581 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,455
author_reputation143,180,130,833,688
root_title"Filter in c++ mit Lambda nutzen"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,456,664
net_rshares12,340,305,341,558
author_curate_reward""
vote details (61)
@dotwin1981 ·
!LOL
!PIZZA
!BBH
properties (22)
authordotwin1981
permlinkre-ozelot47-s1k70h
categoryhive-121566
json_metadata{"tags":["hive-121566"],"app":"peakd/2023.9.2"}
created2023-09-25 20:45:54
last_update2023-09-25 20:45:54
depth1
children2
last_payout2023-10-02 20:45:54
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_length16
author_reputation334,092,684,444,414
root_title"Filter in c++ mit Lambda nutzen"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,471,900
net_rshares0
@bbhbot ·
<center><p>@ozelot47! <b>Your Content Is Awesome</b> so I just sent 1 <b>$BBH</b> (Bitcoin Backed Hive) to your account on behalf of @dotwin1981. <sub>(1/1)</sub></p>
<p><img src="https://i.imgur.com/QwsegY0.png"></p></center>
properties (22)
authorbbhbot
permlinkre-re-ozelot47-s1k70h-20230925t204612z
categoryhive-121566
json_metadata"{"app": "beem/0.24.26"}"
created2023-09-25 20:46:12
last_update2023-09-25 20:46:12
depth2
children0
last_payout2023-10-02 20:46:12
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_length226
author_reputation2,740,199,634,396
root_title"Filter in c++ mit Lambda nutzen"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,471,909
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>I'm glad I know sign language<br>It's pretty handy.</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>(4/10)<br>ENTER @<a href='https://peakd.com/@win.hive/posts'>WIN.HIVE'S</a> DAILY DRAW <a href='https://peakd.com/@win.hive/posts'>AND WIN HIVE!</a></center></p>
properties (22)
authorlolzbot
permlinkre-re-ozelot47-s1k70h-20230925t204641z
categoryhive-121566
json_metadata"{"app": "beem/0.24.19"}"
created2023-09-25 20:46:48
last_update2023-09-25 20:46:48
depth2
children0
last_payout2023-10-02 20:46:48
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_length573
author_reputation196,202,562,917,811
root_title"Filter in c++ mit Lambda nutzen"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,471,936
net_rshares0
@pizzabot ·
<center>PIZZA!


$PIZZA slices delivered:
@dotwin1981<sub>(3/10)</sub> tipped @ozelot47 


</center>
properties (22)
authorpizzabot
permlinkre-filter-in-c-mit-lambda-nutzen-20230925t204631z
categoryhive-121566
json_metadata"{"app": "pizzabot"}"
created2023-09-25 20:46:33
last_update2023-09-25 20:46:33
depth1
children0
last_payout2023-10-02 20:46:33
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_length100
author_reputation7,589,118,567,272
root_title"Filter in c++ mit Lambda nutzen"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id127,471,925
net_rshares0