1.
Introduction
2.
What are Arrays and Vectors?
3.
Syntax
4.
Memory Allocation
5.
6.
Removing elements
7.
Applications
8.
FAQs
9.
Key Takeaways
Last Updated: Mar 27, 2024
Easy

# Vectors vs Arrays

Vidhi Singh
0 upvote

## Introduction

Let's start by rewinding a little and going back a bit(in life xD!) to when we were kids. In those sweet times, whenever someone used to give us a bar of chocolate, and when we didn't feel like eating it right then, we used to add that one to our collection of chocolates, right? There used to be this pretty box wherein we kept all of them. Did you have a separate box for each of the chocolates? No!

Arrays and vectors are these boxes in some computer languages, with the feature where all elements are arranged in a linear fashion. They are two very essential data structures. Data Structures are a way of keeping data. They can be said a kind of tool that stores data in an organized way and maintains certain relationships to leverage them effectively when required.

## What are Arrays and Vectors?

Arrays and vectors store elements linearly, one element after the other. These elements should have the same data type.

At this point, you may think, are they the same? The answer is No.

Let's have a look at the differences.

Let's first have a look at their basic definition:

Arrays: They are a list of elements of the same data types stored in the contiguous memory locations.

Vectors: They are arrays with dynamic size. They have the ability to resize when more elements are added than what memory has been reserved while declaring or initializing. But with this, they are also stored in contiguous memory locations.

## 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.

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

1. Are vectors available in the C language?
No, they are only available in C++.

2. 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.

3. 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.