I have a vector for storing the pointers of all objects (created by new) of a class.
I have an int member variable (called id) for each object which stores the index of the vector element in which the address of that object is stored.
This method helps me to call any object with the help of id.
The id is assigned in the constructor with the help of a static variable, which is incremented each time an object is created.
My problem is that when i delete an object, i cannot delete the vector element (free the memory taken by that element, as my vector is created dynamically) (because if i delete it, then the indexes of all the next elements will decrease by 1).
So I would like to know a method by which i can free the memory from the vector but not cause change in indexes of other elements.
Please tell me a method which doesn’t consume a lot of time. (methods like rearranging elements & then changing id of each object will consume a lot of time!)
I may have around 1000 such objects which may consume a lot of time if i delete the 1st element & do rearrangement as mentioned above.
THANK YOU
I have a vector for storing the pointers of all objects (created by new
Share
You might want to consider using a
map<int,your_object_type*>instead of avector<your_object_type*>. Then you won’t need to ‘reindex’ or (more importantly, I think) reassign IDs when an item is deleted.