mercredi 30 novembre 2016

How to replicate map, filter and reduce behaviors in C++ using STL?

I suppose we can use std::transform to replicate the map behavior in C++ like this :

std::vector<int> in = { 1 , 2 , 3 ,4 }; 
std::vector<int> out(in.size()); 

std::transform(in.being() , in.end() , out.begin() , [](const int & val)
{
    return val+1;
});

I guess a better way would be to use the back inserter.'

std::vector<int> out2;

std::transform(in.begin() , in.end() , std::back_inserter(out2) , [](const int & val){
      return val + 1;
});

// out will be { 2 , 3 ,4 ,5 }

Am I right ? How do I do the filter and reduce operations in C++ using STL ?

Aucun commentaire:

Enregistrer un commentaire