Yes, this is allowed if std::vector not empty. If vector empty, vec[0] will call Undefined Behavior.
std::vector is required to store elements contiguously.
There is also a data( ) method, but it is only C ++ 11.
Important:
This will not work on std::vector<bool> (bit-efficient specialization). But it is also not a container, but IMO it should be deprecated.
milleniumbug
source share