Proper vector memory management

后端 未结 6 1141
情歌与酒
情歌与酒 2021-02-15 11:56

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:36

    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.

提交回复
热议问题