Hello, does anyone know where i can find some good examples on the list::erase() command. Specifically when erasing elements of a list whilst looping through it with for..next ,etc..

Example:. What specifically don't you understand about it?

Hello, the code below (which was very kindly modified by somebody on-line )erases the objects contained in a list tempList that meet the 'if' condition. It works fine , but i don't understand how?. It basically steps through the for..next loop. I think the 'else' statement is confusing me.

void KillMutants2(list < mutantType> &tempList)
 {
    
	 std::list < mutantType> ::iterator i; 
 for (auto it=tempList.begin();it!=tempList.end();)
  {
  if (it->getAge()==3 && it->IsMutant())
  {
    cout << "\nBunny " << it->getName() << " has died of old age";
    it=tempList.erase(it);
  }
  else  ++it;

  }
 }

Use of the iterator is very similar in concept to normal integers.

erase() returns an iterator to the item in the list that follows the item that was deleted. If nothing needs to be deleted than the else statement on line 13 take affect and the iterator is incremented.

Ok I get that ... Doesn't the for .. Next statement take care of incrementing
The iterator if the condition isn't true?

No -- do you see ++it in for (auto it=tempList.begin();it!=tempList.end();) ? I don't.

Okay I didn't notice that , but I'm not familiar with the 'auto' statement either and wondered
if that had anything to do with incrementing IT. Thanks

auto is new to c++0x, which AFAIK has not yet been approved by the standard committee. So, for now, its compiler specific.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.