Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: SFML Book – An interacting world chapter  (Read 1546 times)

0 Members and 1 Guest are viewing this topic.

Noirad

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • www.darionmccoy.com
SFML Book – An interacting world chapter
« on: January 29, 2014, 06:08:33 pm »
I am reading the SFML development book and have a question regarding the "An interacting world" chapter and the section which focuses on cleanup.

Quote
The removal is performed by the following method. In the first part, std::remove_if() rearranges the children container, so that all active nodes are at the beginning and the ones to remove at the end. The call to erase() actually destroys these SceneNode::Ptr objects ...

Why is remove_if() used to rearrange the children container, separating active nodes and ones to be removed? What benefit or security (if there is one) comes with reorganizing the container? This isn't exactly explained why this is done versus another method (i.e. why wouldn't we just mark and remove each object in its original position, rather than organizing the container? ), but seems very specific. Is the method used in the book a standard or is this the only possible solution to managing your objects with a container?
twitter/tumblr: @darionmccoy

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: SFML Book – An interacting world chapter
« Reply #1 on: January 29, 2014, 08:59:52 pm »
i.e. why wouldn't we just mark and remove each object in its original position, rather than organizing the container?
You can't do that. std::vector stores its elements in a contiguous dynamic array, so removing an element also affects others.

We don't use erase() on single elements because it's inefficient (requires copying of all elements behind), especially in an iteration loop (quadratic time complexity).


Is the method used in the book a standard
It's so common that it deserves its own Wikipedia article. The technique is known as erase-remove idiom.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Noirad

  • Newbie
  • *
  • Posts: 14
    • View Profile
    • www.darionmccoy.com
Re: SFML Book – An interacting world chapter
« Reply #2 on: January 29, 2014, 10:45:37 pm »
Got it, now I understand. Thanks a lot.
twitter/tumblr: @darionmccoy

 

anything