How to move all the pointers from one vector to another?

  • A+
Category:Languages

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:

  1. While looping through vec1 simply erasing what I need to remove from it:

    it = vec1.erase(it)

  2. While looping through vec1 moving the last item to the actual index and poping_back

    vec1.at(index) = vec1.back(); vec1.pop_back();

  3. 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());

  4. 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.

Apparently when doing it the 4th way, the pointers get invalidated :(

I would love to see what you guys suggest me. A big thank you to everyone that is willing to help!

 


You can just use std::remove_if to conditionally remove items from a vector. This algorithm will shift items that need to be kept over to the front. Follow it up with a std::vector::erase call to actually remove the items not shifted to the front.

This is similar to your option 3, but you don't need to set an attribute first - just use a predicate that determines if the item should be kept or not, and avoid having to pass over the vector twice.

If you don't want to do it in-place, but want to fill a new vector, then std::copy_if does that.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: