Does insertion of elements in a vector damages a pointer to the vector?

后端 未结 6 594
心在旅途
心在旅途 2021-01-13 10:19

In a program to simulate logic gates I switched from using arrays

node N[1000];

to vectors

vector N;
<         


        
6条回答
  •  被撕碎了的回忆
    2021-01-13 10:47

    When a vector grows, it is reallocated, which effectively invalidates all pointers to elements of the vector.

    If you know beforehand how many elements you will have in the vector, you could use the reserve() method to preallocate space.

提交回复
热议问题