Why am I getting “vector iterators incompatible”?

后端 未结 1 457
别跟我提以往
别跟我提以往 2020-12-16 18:33

Why does this code

#include 
#include 
#include 

int main()
{
    std::vector v;
    v.push_back(1         


        
相关标签:
1条回答
  • 2020-12-16 19:34

    Iterators corresponding to elements are only invalidated when the vector has to be reallocated, which reserve avoids.

    However, v.end() won't stay valid.

    The Standard's description of push_back and insert guarantees that

    Causes reallocation if the new size is greater than the old capacity. If no reallocation happens, all the iterators and references before the insertion point remain valid.

    v.end() is not "before the insertion point".

    0 讨论(0)
提交回复
热议问题