mercredi 6 juin 2018

Iterating through a vector and deleting contents

Is this a good or standard practice to code like this to loop through a vector while deleting unwanted elements from it without losing performance. If there is a faster way please suggest it. This vector is of the form std::vector<AnimationState*> activeAnimations;

void AnimationPack::removeDeadAnimations()
{
    int counter = 0;
    std::remove_if(activeAnimations.begin(), activeAnimations.end(), 
        [&](AnimationState*& animation) {
            if (animation->isActive())
            {
                counter++;
                return true;
            }
            else
                return false;
        });
    activeAnimations.erase(activeAnimations.end() - counter, activeAnimations.end());
}

Aucun commentaire:

Enregistrer un commentaire