Difference between Array and Vector in C/C++

Data Structures can be defined as the collection of data elements that help in storing and manipulating data in an organized manner. Arrays, Vectors, Linked List, Stack, Queue, etc. are all types of data structures. Data structures are the bread and butter of any programming language. It is very important for a programmer to understand the different data structures and their applications.

Data structures are used in almost every sphere of Computer Science such as Artificial Intelligence, Operating Systems, Databases, Networks, graphics, etc. Day by day, the programmers are discovering new algorithms to reduce the time and space complexities.

To ensure that these algorithms work properly, the programmer must choose the right data structure. It would also help in proper data management and increase the efficiency of the algorithm. As a C/C++ programmer, one often gets confused between array and vector. Let us briefly understand the various concepts related to these two in detail.

Difference between Array and Vector

● A Vector is a sequential-based container whereas an array is a data structure that stores a fixed number of elements (elements should of the same type) in sequential order. Vectors are sometimes also known as dynamic arrays.

● Arrays can be implemented in a static or dynamic way whereas vectors can only be implemented dynamically.

● Arrays have a fixed size whereas vectors have a dynamic size i.e they can resize themselves. Whenever you add or remove elements from the vector, the size automatically changes.

● Vectors occupy more memory than arrays since they are dynamic in nature.  Arrays are memory-efficient data structures that use the space allocated to them after they are initialized.

● Accessing the elements of the array takes less time than accessing the elements of the vector because in an array the elements are stored in consecutive memory locations.

● In arrays, the lowest address is provided to the starting element of the array whereas the highest address is provided to the last element of the array. Therefore arrays are also called index-based data structures. Whereas, vectors are not index-based data structures.

● Arrays are available in both C and C++, whereas vectors are only available in C++.

● As soon as the variable goes out of scope the vectors are automatically deallocated from the heap memory whereas we need to explicitly deallocate dynamic arrays.

● The syntax for declaring an array:

int test_array[50];

The syntax for declaring a vector:

vector <int> test_vector;

Array vs Vector – Comparison Table

Array Vector
An array is a data structure that stores a fixed number of elements (elements should of the same type) in sequential order. A Vector is a sequential-based container.
Arrays can be implemented in a static or dynamic way. Vectors can only be implemented dynamically.
Arrays have a fixed size. Vectors have a dynamic size i.e. they can resize themselves.
Arrays are memory-efficient data structures that use the space allocated to them after they are initialized. Vectors occupy more memory than arrays.
Accessing the elements of the array takes less time than accessing the elements of the vector. Accessing the elements of the vector takes more time than accessing the elements of the array.
Index-based data structures. Vectors are not index-based data structures.
Available in both C and C++. Only available in C++.
We need to explicitly deallocate dynamic arrays. Vectors are automatically de-allocated from the heap memory.

What is Array?

An array is a set of identical data items stored at contiguous memory locations that can be accessed randomly using array indices in any programming language. They can be used to store a list of primitive data types of the same kinds, such as int, float, double, char, and so on. In addition, a C/C++ array can store derived data types like the structures, pointers, etc.

Array indexes allow for random access to objects.  Since it generates a single array of multiple elements, it needs fewer lines of code.  All the elements are readily available. Sorting becomes simple because it can be done with fewer lines of code.

However, there are few disadvantages of arrays. It allows for the entry of a predetermined number of elements at the time of declaration. An array in C, unlike a linked list, is not dynamic.  Since the elements must be handled in accordance with the new memory allocation, the addition and deletion of elements can be expensive.

What is Vector?

Vectors are like dynamic arrays in that they can automatically resize themselves when an element is added or removed, and their storage is handled by the container.

Iterators can access and traverse vector elements since they are stored in contiguous storage. Data is usually inserted at the end of vectors. Inserting at the end requires more time since the array can need to be extended at times. Since there is no resizing, removing the last part takes a constant amount of time.

In terms of time, adding and erasing at the start or in the middle is linear. Vector is a template class that is only available in C++, while arrays are a built-in language construct that is available in both C and C++.

Vectors are dynamic arrays with a list interface, while arrays may be statically or dynamically implemented with a primitive data type interface. It can be copied/assigned and passed to any function.

Author
Riten Bhagra
VIT, Bhopal

References

1. https://iq.opengenus.org/array-vs-vector-cpp/
2. https://www.educative.io/edpresso/cpp-vector-vs-array
3. https://stackoverflow.com/questions/15079057/arrays-vs-vectors-introductory-similarities-and-differences
4. https://www.educba.com/c-plus-plus-vector-vs-array/
5. https://www.careerride.com/Java-QA-vector-vs-array.aspx
6. https://www.javatpoint.com/difference-between-arraylist-and-vector

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.