mardi 5 janvier 2016

How to erase elements from a vector based on a specific condition in c++11

I have a vector of objects and I want to erase some objects, without re-sorting.

I found some solutions here, but these are based on comparing the vector element to a value. [How to erase a value efficiently from a sorted vector?

However, I need to erase based on a conditional statement, so I don't think I can use those functions the way they are.

In this example, I have a vector of 3D vectors I need to remove all elements which have a Z value less than 0;

What I have right now is another vector that is created from an original one:

for (int i = 0; i < original_vectors.size(); i++)
        if (original_vectors[i].z > 0)
            new_vectors.push_back(original_vectors[i]);

What can I do to simply drop the elements that don't have Z > 0?

Aucun commentaire:

Enregistrer un commentaire