Are std::vector elements guaranteed to be contiguous?

前端 未结 7 2124
不知归路
不知归路 2020-11-22 03:41

My question is simple: are std::vector elements guaranteed to be contiguous? In order word, can I use the pointer to the first element of a std::vector as a C-array?

<
7条回答
  •  广开言路
    2020-11-22 04:10

    As other answers have pointed out, the contents of a vector is guaranteed to be continuous (excepting bool's weirdness).

    The comment that I wanted to add, is that if you do an insertion or a deletion on the vector, which could cause the vector to reallocate it's memory, then you will cause all of your saved pointers and iterators to be invalidated.

提交回复
热议问题