vector pointer locations guaranteed?

前端 未结 7 1669
无人及你
无人及你 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:38

    No - the vector can be reallocated when it grows. Usually once the vector doubles in size.

    From the C++11 standard

    1 Remarks: 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. If an exception is thrown other than by the copy constructor, move 
    constructor, assignment operator, or move assignment operator of T or by any 
    InputIterator operation there are no effects. If an exception is thrown by the move 
    constructor of a non-CopyInsertable T, the effects are unspecified.
    

提交回复
热议问题