LASER BEAR ASSAULT UNIT LASER BEAR ASSAULT UNIT - 1 month ago 6
C++ Question

using vector to store entites

I'm making a small game and I'm atempting to use vector as a container to store all entites in the world. However I need a way to frequently insert and delete entites.
I'm currently using this to delete entities:

std::swap(m_entities[index], m_entities.back());
m_entities.pop_back();
delete m_entities[index]


But I have no way of checking if an item exists so it crashes if I accidently try to remove item. Also the index is stored in the entity so the index will be invalid for the swapped entity right?
Should I use an unordered_map instead?

Answer

You should analyse the real usage of your container, then refer a benchmark, e.g. this one:

http://baptiste-wicht.com/posts/2012/12/cpp-benchmark-vector-list-deque.html

A more general STL complexity page is here:

https://john-ahlgren.blogspot.de/2013/10/stl-container-performance.html

Comments