Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actuallride

Posted 07 November 2012 - 06:53 AM

You could use a list, however, vector is better in this situation.
Why? Vector is optimized for iterating over the elements. List is optimized for inserting elements into the middle of the container.
Which will you be doing more? (Hint: You'll iterate over the entire list at least once a frame to draw the bullets. You'll probably only add a bullet only once every several frames)

Both, if used properly, can speedily add new elements to the end (vector does it speedily if the capacity is large enough, list always does it speedily). List is better for re-ordering, inserting, or removing. Vector, using the swap-to-end technique mentioned by everyone above, can pop and add easily enough when you don't require your elements to be in a specific order - which in this case you don't require.

Ok vector is better in that aspect, but I was asking if I pop an element out from a list, does list free its memory unlike vector? A vector doesn't shrink even if I pop an element out, so it doesn't have to re allocate the memory.

So for a list, to add a bullet, it first uses new to allocate a space, and then constructs the bullet object.
But for a vector, it constructs the bullet object right away because memory is already allocated.

Am I right?

#1lride

Posted 07 November 2012 - 06:42 AM

You could use a list, however, vector is better in this situation.
Why? Vector is optimized for iterating over the elements. List is optimized for inserting elements into the middle of the container.
Which will you be doing more? (Hint: You'll iterate over the entire list at least once a frame to draw the bullets. You'll probably only add a bullet only once every several frames)

Both, if used properly, can speedily add new elements to the end (vector does it speedily if the capacity is large enough, list always does it speedily). List is better for re-ordering, inserting, or removing. Vector, using the swap-to-end technique mentioned by everyone above, can pop and add easily enough when you don't require your elements to be in a specific order - which in this case you don't require.

Ok vector is better in that aspect, but I was asking if I pop an element out from a list, does list free its memory unlike vector? A vector doesn't shrink even if I pop an element out, so it doesn't have to re allocate the memory.

So if a list free its memory, to add a bullet, it first uses new to allocate a space, and constructs the bullet object.
But for vector, it constructs the bullet object right away.

Am I right?

PARTNERS