What Advantages does a Vector Offer Over an Array in C++?
- Vectors are dynamic, i.e., their size can be changed. Vectors always get space in the heap section of the memory, whereas array size is fixed.
- Vectors can occupy reserved memory space, whereas arrays cannot. Using the reserve() method, we can pre-allocate memory if we know the required size beforehand; this is helpful because it avoids repeatedly allocating memory whenever the space runs out.
- The size of a dynamically allocated array cannot be determined easily, but vector size can be determined in constant time.
- We can return vectors from a function but not an array unless we utilize a dynamically allocated new array.
Difference Between a Vector and an Array
Vectors are Dynamic, Whereas Arrays can be Declared Statically or Dynamically.
The below example explains the difference between a C++ vector and an array in the above sense.
Explanation:
Firstly, we declared a statically allocated array of size 100, then we dynamically allocated an array arr of the size 100 using the new keyword, and finally, we declared a vector of integers.
Array Size is Fixed, but Vectors are re-sizeable, i.e. We can Increase or Decrease Vector Size.
The example below explains the difference between a C++ vector and an array in the above sense.
Output:
Size of array arr: 100
Size of the vector before removing a value: 5
Size of the vector after removing a value: 4
Explanation:
Firstly, we created an array of integers of size 100 and displayed its size. Then, we created a vector of integers and inserted 5 values in it, and we displayed their size. Finally, we removed one value from the vector using the erase function and again displayed its size.
Memory Allocated to a Dynamically Created Array has to be de-allocated Explicitly, but Memory Allocated to Vectors gets de-allocated Automatically.
The example below explains the difference between a C++ vector and an array in the above sense.
Explanation:
Firstly, we declared an array dynamically of size 100 and then explicitly de-allocated its memory since it was being used. Then, we created a vector whose memory will be automatically de-allocated when it goes out of scope, in this case, when the main function ends.
We can return Vectors from a Function but only One Array If We Utilize a Dynamically Allocated New Array.
The below example explains the difference between a C++ vector and an array in the above sense.
Output:
Segmentation Fault
Explanation:
Firstly, we declared an integer pointer and then called the insertValues function to insert values in the array. In the insertValues function, we declared a static array of size 10, inserted values in it, and returned it from the function. Since it was a static array, a compilation error occurred; we cannot return a statically allocated array; it has to be a dynamically allocated array.
Arrays Cannot be Copied Directly, but Vectors Can
Output:
Explanation:
Firstly, we declared a vector of integers and inserted some values in it, and then we declared another vector named copy and assigned v to copy; it produced no error. Then we statically declared an array of size 5 and inserted values in it, and then we declared another array copyArray and tried to assign array to copyArray, but it produced a compilation error since it could not be done.
Conclusion
- Vectors are re-sizeable, whereas static arrays are not.
- Arrays cannot be copied directly but vectors can.
- Deallocation of the memory for the statically and dynamically declared arrays has to be done explicitly whereas, in the case of vectors, it is done automatically.
- We can return vectors from a function but not an array unless we utilize a dynamically allocated new array.