jeudi 27 avril 2017

shrink_to_fit() vs swap trick

I have a game where certain game objects spawn all at once and then despawn as they get destroyed/killed. The game objects are elements in an std::vector, and I'd like to minimize memory usage. I'm used to the swap trick,

std::vector<gameObject>(gameObjectVector.begin(), gameObjectVector.end()).swap(gameObjectVector);

but I noticed the inbuilt shrink_to_fit() from C++11. However, it has linear complexity while the swap trick is constant. Isn't the swap trick superior in every way?

Aucun commentaire:

Enregistrer un commentaire