I am struggling a bit to understand the best way to safely store a reference or pointer to an item in a container without risking that the pointer could get invalidated.
With std::vector
this seems like a bad idea in general, since you never know when your item might be moved to a new memory location as vector
re-allocates. Is there such a way?
With list
and deque
is this instead a better option? From what I understand, items in these collections are not moved around, so could I just take a pointer to the memory address of a specific element?
I'm coming from Objective-C where this sort of thing is easy and common with all its containers since Objective-C pretty much uses pointers for everything so tossing them around and copying them is quite commonplace.