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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, that’s fine. As of C++03,
vectoris required to have contiguous storage.As of C++11, the same is true for
std::string, by the way; and you can sayv.data()as a synonym for&v[0](which is also valid whenvis empty).