Can vector iterators in c++ be corrupted when you add elements?

This question already has an answer here:

  • Iterator invalidation rules 4 answers

  • Yes.

    Adding elements to a vector can cause reallocation, which will invalidate all iterators and pointers to the vector elements.


    Yes, this can happen. If the vector resizes old iterators are no longer valid. This is not true for all collections.

    Take a look at the invalidation rules.

    链接地址: http://www.djcxy.com/p/73336.html

    上一篇: 将指针存储在std :: set中安全吗?

    下一篇: 添加元素时,c ++中的矢量迭代器是否会损坏?