I am using a std::vector to store an array of objects that are referenced from outside of the vector by other objects. I drew a diagram to explain more clearly:

I am storing objects rather than pointers for performance reasons. These objects are sorted every frame of my game, so I want the array to have good cache properties.
Of course, whenever objects are added to the vector, there is a chance that the array will be reseated. In that case my references are invalidated and need to be updated. Right now, to detect the reseating, I am using the following method:
size_t old_capacity = v.capacity();
// Do stuff that could change the vector's size
v.push_back(a);
v.push_back(b);
v.push_back(c);
if (old_capacity != v.capacity()) {
update_references();
}
My questions are:
- Is this the best way to detect that the vector has reseated its array?
- Do I also need to check for reseating after performing
pop_back?
In my mind, the best method would be the following: Just use the pointer to the head of the vector. Not all reallocations will cause the vector to move.
However, I more or less agree with the comments about references into your vector. Also, you could use a std::list which wouldn’t have the problems with reallocations.