Proper vector memory management

后端 未结 6 1222
面向向阳花
面向向阳花 2021-02-15 12:08

I\'m making a game and I have a vector of bullets flying around. When the bullet is finished, I do bullets.erase(bullets.begin() + i); Then the bullet disappears. However it doe

6条回答
  •  我在风中等你
    2021-02-15 12:15

    It may not get rid of the memory.
    But next time you need to add a bullit it does not need to re-allocate more space.
    It will not re-use the memory that the erased bullet came from.

    Note:
    If you are erasing from the middle of a container relatively often then vector may not be the correct container. This is becuase if you remove element n then all the elements from [n+1,end) must be moved down one space in memory.

提交回复
热议问题