Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


#ActualParadigm Shifter

Posted 04 May 2013 - 07:30 AM

You can erase items from a list as your iterating it, you just need to stash the next iterator before you do stuff to the current one.

 

auto it = myList.begin();

while(it != myList.end())
{
    auto nextIt = it;
    ++nextIt;

    doStuffWith(it);

    it = nextIt;
}

I'm using auto to save typing but if you not got C++11 support it's easy to change.

 

EDIT: This wont work with vector or any other container that can cause stuff to be copied or moved around in the container.


#1Paradigm Shifter

Posted 04 May 2013 - 07:29 AM

You can erase items from a list as your iterating it, you just need to stash the next iterator before you do stuff to the current one.

 

auto it = myList.begin();

while(it != myList.end())
{
    auto nextIt = it;
    ++nextIt;

    doStuffWith(it);

    it = nextIt;
}

I'm using auto to save typing but if you not got C++11 support it's easy to change.


PARTNERS