vector pointer locations guaranteed?

前端 未结 7 1731
无人及你
无人及你 2021-01-05 01:05

Suppose I have a vector of ints,

std::vector numbers;

that is populated with a bunch of values, then I say do this (where an ent

7条回答
  •  长情又很酷
    2021-01-05 01:33

    Is oneNumber guaranteed to always be pointing at the int at index 43

    Yes, this is guaranteed by the standard.

    even if say I resize numbers to something like numbers.resize(46)?

    No. Once you resize, add, or remove anything to the vector, all addresses and iterators to it are invalidated. This is because the vector may need to be reallocated with new memory locations.

提交回复
热议问题