Quantcast
Channel: GameDev.net
Viewing all articles
Browse latest Browse all 17560

Remove any items from a list while iterating

$
0
0

Has anyone found a way to remove any item from a list as it is iterating?

Normally a loop to iterate through a linked list looks like this:

for ( it = list.begin(); it != list.end(); it++ )
{
}

 

 

If you want to be able to remove an item from the list while it is iterating, you can always do this:

it = list.begin();
while (it != list.end() )
{
	if (condition)
	{
		it = list.erase(it);
	}
	else
	{
		it++;
	}
}

 

 

But what if the code in your loop called other code that could result in the removal of the object from the list …


Viewing all articles
Browse latest Browse all 17560

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>