arrays vs vectors

I have just recently learned arrays. After arrays i learned about vectors , they seem so similar. Should i use vectors or arrays?
It depends on the context. For example if you know the maximum number of elements and want to define objects in the stack then it is better to use arrays. Otherwise it is better to use vectors.
Last edited on
The clear difference between the 2 is that an array is a fixed size and a vector is variable...
Arrays should be more efficient and take up less room due to them having a few less functions and members but the difference is tiny.
Most people just use arrays anyway for nearly everything unless it specifically needs to change in size.
vector can save on the container.
like the size will read and save as array in the container as long u didn't limit the container

else the arrays sometime you you have to limit it and quite make troublesome
The C++ Coding Standards ( ) says:
item 76: Use vectors by default
item 77: Use vector and string instead of arrays

It's really difficult to come up with justified use of arrays, especially in modern C++ (but if you do, then use them, of course. Item 76 says just that: "If you have a good reason to use a specific container type, use that container type knowing that you did the right thing" and Item 77 adds: "An array can be acceptable when its size really is fixed at compile time".

Don't forget that vectors are a lot faster than arrays if you ever need to swap or move them around (e.g. to rearrange a container of them)
Last edited on
Use std::string instead of char[].
Use std::vector instead of pretty much any other kind of array.
Use std::array for when you don't need to be able to resize (careful, these have a size limit).
Use std::unique_ptr for dynamically allocated arrays.
Topic archived. No new replies allowed.