mardi 2 juin 2020

Store result in a zip view with ranges

I have two vectors and for both of them I want to apply a transform based on a third vector

std::vector<int> out1;
std::vector<int> out2;
std::vector<int> in1;

std::transform(std::begin(in1), std::end(in1), std::begin(out1), transform1);
std::transform(std::begin(in1), std::end(in1), std::begin(out2), transform2);

This needs two loops, so I have tried to do it using a zip range from ranges-v3

std::vector<int> out1;
std::vector<int> out2;
std::vector<int> in1;
auto &zip = ranges::zip(out1, out2);

std::transform(std::begin(in1), std::end(in1), std::begin(zip), transform1and2);

This however has the drawback of embedding the transformation for 1 and 2 in the same function, needing to modify it to return an std::pair.

Is there any cleaner way to do this?

Aucun commentaire:

Enregistrer un commentaire