C++11 (and C++03 for that matter) guarantees that the data in std::vector
are contiguous. This means, that in your case, &my_vector[0]
can be thought of as the pointer to the zeroth element of that array and normal pointer arithmetic can be used to access the other elements. It's preferred to use my_vector.data()
to recover the pointer to the zeroth element.
C++98 almost guarantees it.
Note that it's not true for std::vector<bool>
for any of the C++ standards. This is because std::vector<bool>
is implemented as a particular specialisation.