Skip to content

Instantly share code, notes, and snippets.

@gregkepler
Last active December 2, 2016 16:39
Show Gist options
  • Save gregkepler/ceb6a6f6f017affd3a93eb678c0fa885 to your computer and use it in GitHub Desktop.
Save gregkepler/ceb6a6f6f017affd3a93eb678c0fa885 to your computer and use it in GitHub Desktop.
Iterate through C++ vector updating active objects and erasing ones marked for removal.
void update()
{
// iterate through all objects in the vector
for( auto iter = mObjectVector.begin() ; iter != mObjectVector.end(); /*DO NOT INCREMENT HERE*/ )
{
if( (*iter)->isMarkedForRemoval() ){
// Remove if obsolete and iter will be the following object in vector
iter = mObjectVector.erase( iter );
} else {
// Update if still active
(*iter)->update();
// increment here
iter++;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment