I am usually unsure when it is better to use one versus the other. They both seem to do the same things in general but is vector more flexible in terms of what it can do? When are arrays more appropriate?
Share
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.
Generally always prefer using
std::vector<T>since the destruction will be automatic once the vector goes out scope and the allocated memory will be placed neatly on the heap and all the memory will be handled for you.std::vector<T>gives you everything you get in an array and even a guarantee that the elements will be contiguously stored in memory (except forstd::vector<bool>).In the case of
std::vector<bool>you have to be careful since code like this will break:Fact is,
std::vector<bool>doesn’t store contiguousbools. This is an exception in the standard that is fixed in C++11.