Jump to content
  • Advertisement
Sign in to follow this  
endasil

Iterators and Delete?

This topic is 4834 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

//--------------------------------------------------------------------
// Animate entities
//--------------------------------------------------------------------

for(int iLayerNr = 0; iLayerNr < MAX_ENTITY_LAYERS;iLayerNr++)
{

    map<uint,CEntity*> ::iterator next = mEntities[iLayerNr].begin();

    while(next != mEntities[iLayerNr].end())
    {
	int retval = 0;
	
        if(next->second->hasAttribute(CEntity::ATTRIBUTE_ANIMATED))
	{
		retval = next->second->update(newtime, lasttime);
	}
	if(retval == -1)
	{

        //--------------------------------------------------------------
	//Returned -1, entity wants us to delete it...
	//--------------------------------------------------------------------
	        delete next->second;
		map<uint,CEntity*> ::iterator curr = next++;
	        mEntities[iLayerNr].erase(curr);
	}
	else
	{	
	        next++;
	}
    }
}


the question i have is about this part. It seems like he is erasing the next entity and not the one that requested to be erased?

        delete next->second;
	map<uint,CEntity*> ::iterator curr = next++;
        mEntities[iLayerNr].erase(curr);
should it not be like this or is there something i missed?

        delete next->second;
        mEntities[iLayerNr].erase(curr);
	map<uint,CEntity*> ::iterator curr = next++;

// edit - added a title [Edited by - LessBread on August 23, 2005 7:01:22 PM]

Share this post


Link to post
Share on other sites
Advertisement
No, you are reading the code literally. I suppose "next" is a misnomer. It would have been better named as "current".

You may want to read up on STL iterators, particularly iterators for maps, which use std::pairs to deal with the pointed to element (and thusly the use of next->second in the code which can be even more misleading).

Where does this code come from?

[edit] I did not read everything. Also note that in the line map<uint,CEntity*> ::iterator curr = next++;, the author is using the postfix increment operator, so curr is getting the value of next and then next is being incremented. It is a little strange in the readability department, but it does what it is supposed to do. I would have done something like this instead:

delete next->second;
next = mEntities[iLayerNr].erase( next );



jfl.

Share this post


Link to post
Share on other sites
Ah, next++ increases after the value has been given, that changes things. I see the code very differently now that i know that. Thanks alot!

The code comes from my uni teacher in 2d game engine development, who is currently on vacation so i could not ask him about his code.

Share this post


Link to post
Share on other sites
For clarification:

If you did not want the value to be incremented after assignment, you would use the prefix increment operator, that is ++next instead of next++.

Also, map::erase() -- or at least the form that is in use -- will return an iterator to the next element or the end of the container (map::end()), if there are no more elements. I see no reason for creating a temporary iterator (curr) except to show explicitly that the next iterator has changed. In this case, however, it seems to have made things more confused.


jfl.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!