Let say we have:
std::vector<Segment*> segments;
...
Segment* dd = new Segment;
segments.emplace_back(dd);
Owner* owner = getOwner();
owner->setSegmentPointer(&(segments.back());
This will not work because of Iterator invalidation rules.
Any subsequent element addition to the vector segments
will render invalid the pointers stored in owner
. How to circumvent this problem keeping the access time of std::vector<>
? (Assuming that we can't use vector.resize
in advance). Is to use std::map<>
the only solution?
Thanks in advance