Basically what I want to do is remove some of the pointers inside my vector, but I found out that it can be quite slow to do that in the middle of the vector.
So I have a vector that already has data inside:
std::vector<Class*> vec1; // This already contains pointers
I'll iterate through vec1 and will add some of the pointers to another vector (vec2): vec2.push_back(vec1.at(index))
Now I would like to do is something like vec1 = vec2
but I don't know if this is the better (effecient) way to do that.
What would be the best way to do that?
I tried:
-
While looping through vec1 simply erasing what I need to remove from it:
it = vec1.erase(it)
-
While looping through vec1 moving the last item to the actual index and poping_back
vec1.at(index) = vec1.back(); vec1.pop_back();
-
Setting some attribute on the object the pointer is pointing while looping through vec1 and than using
std::remove_if
vec1.erase(std::remove_if(vec1.begin(), vec1.end(), shouldBeRemoved), vec1.end());
-
Now I'm trying to generate a new vector while looping through vec1 and adding the pointers I want to keep, then "swapping" or "moving" the contents of this new vector to vec1.
I would love to see what you guys suggest me. A big thank you to everyone that is willing to help!
Aucun commentaire:
Enregistrer un commentaire