Can a std::vector<char>
be treated like an array in this way:
std::vector<char> v(10);
strncpy(&v[0], "hello", 9); // <-- Is this safe?
Yes, that's fine. As of C++03, vector
is required to have contiguous storage.
As of C++11, the same is true for std::string
, by the way; and you can say v.data()
as a synonym for &v[0]
(which is also valid when v
is empty).
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