Can std :: vector be treated as an array

Can std::vector<char> be processed as an array in this way:

 std::vector<char> v(10); strncpy(&v[0], "hello", 9); // <-- Is this safe? 
+8
c ++ vector stl
source share
1 answer

Yes this good. Starting with C ++ 03, vector requires continuous storage.

As in 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 true if v empty).

+13
source share

All Articles