samedi 27 mai 2017

Copy C++ vector but apply lambda to each element

I have got a std::vector X of std::vector of, say, double in C++.

How can I transform X into a std::vector Y of int such that X[i].size() == Y[i] holds for all admissible indices i?

std::vector< std::vector<int> > X;
...
/* What I want to do should look as follows */
std::vector<int> Y = std::copy_and_transform( X, lambda_to_get_size );

Of course, this can be realized with a loop, but in C++11 we would like to use lambdas instead. I have not found anything like that in std::algorithm. The standard tools seem to only give inplace transformations that change the original vector and which do not allow changing the data type.

Aucun commentaire:

Enregistrer un commentaire