An easy way to avoid losing memory and blocking memory blocks together for a better cache hit is to simply save the physics elements that you deleted in the vector to avoid index invalidation, but mark them removed so that you can restore these free spaces on subsequent inserts.
Here, if you want to go the full path of creating your own container, it really helps if you still want to refer to the destructors on these remote objects to understand how STL containers are implemented using "post new" and manual calls to the destructor to avoid the need for things like an assignment operator for type T.
You can additionally direct them to the list of vacant indexes, which will be corrected whenever you insert a new one or even faster, treat one of these elements as a union of a list pointer, for example:
union PhysicsNode { PhysicsBody body; PhysicsNode* next_free; }; PhysicsNode* free_physics_nodes;
The same goes for collision node. In this case, you consider this physical class as physical when it is βbusyβ, and as a separate node list when it is βfreedβ or βfreeβ to be fixed.
Unfortunately, when you try to solve a problem at this level, you often come across object-oriented constructors, constructors and destructors, etc.
Therefore, when you want such efficiency together with all the object-oriented software advantages, when you may encounter the problem of solving this problem, basically the same thing at the level of the memory allocator.
source share