Yes, the elements of a std::vector are guaranteed to be contiguous.
Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays.
Although elements of the vector are stored in the contiguous block of memory, the memory where elements reside is not the part of the vector object itself.
Yes, vector elements are contiguous, if you use c++11 you can also use the function T* data instead of the ugly cast to pass the underlying array to another function.
Yes, that is a valid assumption (*).
From the C++03 standard (23.2.4.1):
The elements of a vector are stored contiguously, meaning that if v is a vector where T is some type other than bool, then it obeys the identity &v[n] == &v[0] + n for all 0 <= n < v.size().
(*) ... but watch out for the array being reallocated (invalidating any pointers and iterators) after adding elements to it.
The C++03 standard added wording to make it clear that vector elements must be contiguous.
C++03 23.2.4 Paragraph 1 contains the following language which is not in the C++98 standard document:
The elements of a
vector
are stored contiguously, meaning that ifv
is avector<T, Allocator>
whereT
is some type other thanbool
, then it obeys the identity&v[n] == &v[0] + n
for all0 <= n < v.size()
.
Herb Sutter talks about this change in one of his blog entries, Cringe not: Vectors are guaranteed to be contiguous:
... contiguity is in fact part of the vector abstraction. It’s so important, in fact, that when it was discovered that the C++98 standard didn’t completely guarantee contiguity, the C++03 standard was amended to explicitly add the guarantee.
Storage is always contiguous, but it may move as the vector's capacity is changed.
If you had a pointer, reference, or iterator on element zero (or any element) before a capacity-changing operation, it is invalidated and must be reassigned.
Yes it's contiguous
std::vector
guarantees that the items are stored in a contiguous array, and is therefore the preferred replacement of arrays and can also be used to interface with platform-dependent low-level code (like Win32 API calls). To get a pointer to the array use:
&myVector.front();
yes.
it should alway be contiguous
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With