I have an std::vector that holds all the objects and is passed to them in every frame, so that every object can access other objects. My question is how can an object can delete itself from the vector?
This doesn’t work:
vector.erase(this);
Is there any other solution for this?
You need to delete the element by accessing its iterator, this can be done through
std::find, or if you know the index (less complexity) through:so that you can delete it:
Mind that, since a vector stores its data in an array format, this causes all elements after the one you are deleting to be shifted to the left (which is expensive). Consider using a
std::listinstead if you don’t need random access.