1

How does an iterator keep up with the changing std::vector location. I know that whenever a new element is added to a full vector, it allocates 2x of new memory and copies all elements to that location. After that it deletes the old copy and add the new element. But how does the iterator know that what is the new address of the vector? How does it update itself? I would like to know the memory implementation for the iterator working.

  • 4
    IIRC vector iterators don't know the new address of the vector - they get invalidated when it reallocates. – Weak to Enuma Elish May 08 '17 at 03:56
  • 4
    The itetator does not update itself after a reallocation, but it get's invalidated. Use a *stable vector*, a map, a set or a list if you need this feature. Or rather work with the indices. – davidhigh May 08 '17 at 03:57
  • Thanks for the help. I did not know that iterator gets invalidated. So i never searched for it. The other post answers all my questions. – saurabh pandey May 08 '17 at 04:21

0 Answers0