vendredi 27 novembre 2015

std::transform on vector of vector

I want to apply std::transform on vector of vector. This transform should do transformation from std::vector<std::vector<Told>> to std::vector<std::vector<Tnew>>.

I tried the following and it worked:

std::vector<std::vector<Told>> source;
//fill source
std::vector<std::vector<Tnew>> dest;
dest.reserve(inliers_pairs.size());
std::transform(begin(source), end(source), std::back_inserter(dest), [](std::vector<Told>& item){
    std::vector<Tnew> new_item;
    new_item.reserve(item.size());
    std::transform(begin(item), end(item), std::back_inserter(new_item), [](Told& inner_item){
        return get_Tnew_from_Told(inner_item);
    });
    return new_item;
});

Is there a direct way to do it in one transformation? Cleaner way? More efficient?

Aucun commentaire:

Enregistrer un commentaire