0

I know that vector does dynamically change the address of stored elements when it is changed by some action like push or pop.

Is that true for the set and queue?

  • 1
    there is no `std::set::push` nor `std::set::pop` – 463035818_is_not_an_ai Nov 22 '21 at 11:35
  • 2
    much related: https://stackoverflow.com/questions/6438086/iterator-invalidation-rules-for-c-containers – 463035818_is_not_an_ai Nov 22 '21 at 11:36
  • 3
    `std::queue` is not a container, it is a container adaptor. So the answer will depend on the type of the underlying container used by queue. – Daniel Langr Nov 22 '21 at 11:36
  • Click on the methods in the __Modifiers__ section here https://en.cppreference.com/w/cpp/container/set . For example `std::set::insert` _"...No iterators or references are invalidated...."_ The so the same for `std::queue`. – Richard Critten Nov 22 '21 at 11:37
  • Or, with `[std::set::erase]`: _"References and iterators to the erased elements are invalidated. Other references and iterators are not affected."_ – Daniel Langr Nov 22 '21 at 11:38
  • Associative containers generally don't invalidate existing iterators to other elements. Queue, stack and priority queue are adaptors over existing containers so they don't really dictate how their operations will behave. vector will invalidate all iterators if reallocation happens, will invalidate all iterators past the element erased or inserted. You should consult the documentation for specifics. – Tanveer Badar Nov 22 '21 at 11:39
  • 2
    Nice table of invalidation rules here - page down to [Iterator invalidation](https://en.cppreference.com/w/cpp/container) – Richard Critten Nov 22 '21 at 11:40
  • @463035818_is_not_a_number thanks that is what I need – jojo_Aero_smith_the_dummy Nov 22 '21 at 11:41
  • @DanielLangr thanks that is what i need – jojo_Aero_smith_the_dummy Nov 22 '21 at 11:41

0 Answers0