+ 47

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?

13th Apr 2017, 5:32 AM
Hatsy Rei
Hatsy Rei - avatar
12 Antworten
+ 55
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.
13th Apr 2017, 6:48 AM
Squidy
Squidy - avatar
+ 37
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.
13th Apr 2017, 6:33 AM
Fox
Fox - avatar
+ 23
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!
13th Apr 2017, 5:55 AM
Dev
Dev - avatar
+ 5
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.
13th Apr 2017, 6:28 AM
Petja Boigk
Petja Boigk - avatar
+ 3
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
8th Sep 2019, 12:23 AM
Spiff Jekey Green
Spiff Jekey Green - avatar
+ 2
Arrays all the way😍😍you can do anything with an array and the fact that its very coservative.
30th Apr 2020, 9:57 PM
rachel oyugi
+ 2
I goes with the vector because it is dynamic in nature
30th Jan 2022, 1:15 PM
Ankit Kumar
Ankit Kumar - avatar
+ 1
Cool
8th Aug 2022, 12:01 PM
MSiva S
MSiva S - avatar
+ 1
Hi
26th Jan 2023, 10:32 AM
0149 Dhruvil
0149 Dhruvil - avatar
0
Hi
29th Jul 2023, 6:54 AM
Kelvin Smith
Kelvin Smith - avatar
0
Yeah
12th Sep 2024, 6:31 AM
Munange Japhet
Munange Japhet - avatar
- 2
Microsoft a crane o.o you are a user
5th Mar 2022, 10:44 AM
Mani Daryan