vendredi 25 septembre 2020

How to concatinate multiple vectors of shared_ptr's?

I've seen other post but I am trying to do this using some of the <algorithm> methods. I have a pointer to a map which contains a key to a vector of pointers to BaseElement classes like the following.

using ElementV = std::vector<std::shared_ptr<BaseElement>>;
using ElementM = std::map<int, ElementV>;
using SElementM = std::shared_ptr<ElementM>;

SElementM elements;

What I am trying to do is concatenate each of the vectors (ElementV) stored as values in the map (ElementM) and populate one large ElementV. I'd like to not have to do deep copies and just access the original elements by their smart pointers (shared_ptr(BaseElement)) from the allElems vector.

The following is wrong, but gives the idea of what I'm trying to do.

ElementV allElems;

for (auto& index : indices) {
    allElems = elements->at(index);
}

I suspect I should be using lambas with std::copy, but have been unable to get something similar to the following to work and I think it's because of iterators.

std::copy(allElems.begin(), allElems.end(), [const elements &elems](std::vector<shared_ptr<BaseElement> &val) {
    elems ...?
}

Thoughts?

Aucun commentaire:

Enregistrer un commentaire