Syntax
For arrays: <data_type> <name_of_the_array>[size];
Here, the size is optional. If it is not initialized, then it is mandatory to provide size; otherwise, it takes the size of the number of elements that have been initialized. When it has not been initialized, it contains the garbage value.
For example:
int ar[3]; //Declaring an array with the name ar, size 3, and 'int' data type
OR
int ar[]={1,2,3}; //Declaration along with initialization
Arrays are present in both C and C++.
For vectors: vector<data_type>(size);
Here, again, size is optional. Although, if the size is not given, it is assumed to be zero(0). In case size is given but not initialized, it holds 'nothing' equivalent of the data type that it has been declared with.
For example:
vector<int> vec(3); //Declaraing vector with name vec, size 3, and ‘int’ data type
OR
vector<int> vec={1,2,3}; //Declaration along with initialization
Vectors are only present in C++.
Memory Allocation
For arrays, memory is allocated in the stack. It is a chunk of memory that has been requested during declaration or initialization. The starting address of that memory is returned after initialization.
For vectors, memory is allocated in the heap as it is a dynamic allocation. The other reason is its resizability feature. A vector eventually takes more space than it actually uses. The whole explanation for this is provided in the next point.
As heap is used for vector memory allocation, its access time is also greater than that of an array as in a heap; more cache misses tend to occur.
Adding elements
For arrays, only that data can be added for arrays for which memory has been reserved earlier. It is not flexible. It takes O(1) time to update any index of the array or add an element.
For vectors, more elements can be added even though memory has not been allocated for it in advance. Functions that enable this to be done are push_back(value), emplace_back(value). These functions add the element at the back of the vector. In order to add elements in middle or start, functions are emplace() and insert(). Both of these functions require an iterator as well as one of the parameters.
Now, you must be wondering how is it possible that vectors can hold more elements than the size of the vector. So, what happens is as soon as there is no space left and a new element is added to the vector, the vector is implicitly allocated a new space in the memory with double the size of what it earlier was, and all its values are copied there. This way, its size increases exponentially. This is also the reason why a vector takes more space than it actually uses.
For example, a vector size is three and it already has three elements. Now a new element needs to be added. To do so, a new space of 6 elements is allocated to it. Now, if no more elements are added to it after this, the left out space is wasted as it is reserved but not used by this vector.
We have explained this on a small scale; imagine this when the size of a vector is very large(like 10000). The time-complexity of adding an element in the vector is O(m), where m is the number of data values that need to be shifted to insert that element. Although, its amortized time- complexity is considered to be O(1).
Removing elements
For arrays, there is no such built-in function to delete elements. Although, such functions can be implemented by the programmer. The most efficient one has O(n) as time-complexity, where n is the total number of elements.
For vectors, functions like pop_back(), emplace_back() remove element from back of the vector and functions like emplace() and erase(), which require iterator as a parameter as well, deletes elements from any place in vector. There is another function clear(), that makes the size of the vector zero(0) by deleting all the elements. Time-complexity of such functions is O(n), where n is the number of elements.
Applications
Arrays should ideally be used when elements have to be accessed a lot of times whereas in scenarios where a lot of operations include addition and deletion of elements vectors should be employed.
FAQs
-
Are vectors available in the C language?
No, they are only available in C++.
-
Which of the arrays and vectors are more memory efficient?
Arrays are more memory efficient as vectors end up taking more space than they use.
-
Is adding more elements than the size of the array possible?
It is not possible to do so. Although, a new array can be declared with more size and then all elements can be copied through some code explicitly.
Key Takeaways
This article explains the differences between vectors and arrays along with their detailed aspects.
Recommended Read:
Array in Java
IEnumerable vs IQueryable
We really hope that this blog has helped you enhance your knowledge regarding vectors and arrays, and if you would like to learn more, check out our articles on link. Do upvote our blog to help other ninjas grow.
Happy Coding!