Arrays VS Vectors | Sololearn: Learn to code for FREE!


Arrays VS Vectors

As we know it, arrays are collections of variables of the same data type. According to C++ Reference, "Vectors are sequence containers representing arrays that can change in size. Just like arrays, vectors use contiguous storage locations for their elements, which means that their elements can also be accessed using offsets on regular pointers to its elements, and just as efficiently as in arrays." If vectors are much superior to arrays, in which conditions would one prefer arrays over vectors?

4/13/2017 5:32:04 AM

Hatsy Rei

9 Answers

New Answer


If you're using dynamic memory, you should go with a vector. Depending on the implementation, there will be a little bit of overhead (begin pointer, end pointer, etc., which won't exist for arrays), but you get the safety and convenience of not having to manage dynamic memory yourself. If you need a fixed amount of memory, then go with an array. A vector will always allocate its memory on the heap*, but if it's always going to have a fixed size, it's much better to use an array to avoid this overhead (since a fixed-size array can exist on the stack). Using an std::array wrapper (C++11) even offers you the convenience of iterators without any added overhead. It's true that regardless of whether you use arrays or vectors, accessing the elements will be "just as efficient" (vectors are just a wrapper over regular arrays, after all). The inefficiency comes from when and how the memory is allocated. *It's not necessarily true that vectors use the heap to allocate memory. This will be the behavior by default, but you can provide your own custom allocator to take memory from a preallocated stack-based pool that you manage yourself.


From what I understand, the preference usually revolves around how much of the stack's memory is sacrificed or needed during execution. To understand this better... take a program that involves using a lengthy algorithm and moving data around. Make two copies of this program.. In one of them, use vectors as the data's medium. In the other, use arrays. If I'm correct, if you were to note the execution time of each program, the program that uses arrays would finish and terminate first. Arrays take up less memory, therefore execution-time is shorter. Also, I don't remember whether or not you have to include a library/header for arrays in order to use them. If you don't.. then that's another plus. Hopefully somebody will correct me if any of this is incorrect. My C++ textbook is 3 feet from me so I'll have a look in a few minutes. To sum this up... it's really a question of what's more important in the program.. pin-point accuracy and organization, or execution-time. Vectors are from the stl library. Arrays are from the std library.


I would go with vectors in this case... Elements in an array cannot be deleted for sure and it cannot store multiple data types as well, Whereas in vectors, elements could be deleted, multiple objects can be stored and the size is changeable! BTW arrays are easy to sort than vectors, but arrays are not synchronizable as compared to vectors!


all the things dayve mentioned can be reasons to use arrays in some cases. not a c++ expert here, so just an educated guess, but generally I use arrays when I want to basically limit the user of an api (even though a appropriate encapsulated object would be better here). Also i think vectors are bigger in size than arrays. so on space-critical system I imagine arrays would be better (not that common anymore). again just an educated guess.


I prefer arrays when I'm dealing with little amount of data and when I'm sure it's not going to grow as time goes on


Arrays all the way😍😍you can do anything with an array and the fact that its very coservative.


Soldra punda


They are different


Vectors are a kinds of arrays.