Difference Between Vector and List


Vector and List are present in many programming languages like C++, R, etc. and they are used to add and remove elements. These elements can be accessed randomly in a vector but not in a list. In this article, we will discuss the difference between vector and list in C++.

What is Vector in C++?

A vector is a container which works as a dynamic array. It can be used to store elements whose datatype is same. Vectors also have a characteristic of growing or shrinking at runtime. Vectors belong to the Standard Template Library (STL) and <vector> header file has to be used to work on vectors.

Vector Example

Here is an example of creating a vector and adding and deleting elements.

vector myvec;
myvec.insert(2);
myvec.delete();

What is a List in C++?

A list is also a container and it is used for storing elements. Its implementation is done in the form of doubly linked lists. A doubly linked list is a list in which each element is connected to its previous and the next element. Access to the elements is slow in comparison to arrays or vectors. Contiguous memory allocation is not supported by lists so memory can be efficiently allocated to the elements as a separate node for each element is required for its storage in a list.

List Example

Here is an example of creating a list and adding and deleting an element.

list mylist;
mylist.insert_begin(2);
mylist.delete();

Difference between Vector and List

Here is the table for the difference between Vector and List.

Vector List
Contiguous memory is supported by Vector. The list supports non-contiguous memory.
Vector supports synchronization. The list does not support synchronization.
The size of a vector may or may not have a default size. The list does not support default size and can be as big as possible.
The space required for each element in the vector belongs to the element only and no extra space is needed. Each element in the list requires extra space for the node which holds it. This space includes pointers which connects an element to the previous and the next element.
The thread related to the vector is safe. The thread related to the list is not safe.
Elements in a vector can be randomly accessed through the [] operator. Elements in a list cannot be accessed randomly.
Iterators do not work properly if an element is added or removed in a vector. The working of iterators is not disturbed even if elements are added or removed.
Vectors are implemented in the form of dynamic arrays. Lists are implemented in the form of doubly linked list.
Inserting or deleting an element in a vector is difficult because elements have to be shifted. Inserting or deleting an element in a list is easy as pointers need to be updated.
The cache performance of vectors works better than lists as the same cache line is used to store the elements. Cache performance is not good as the elements are stored in separate nodes.
Vector can be sorted with the help of the sort() function. No sort() function is available in the list class. The elements in a list have to be sorted manually.

Conclusion

Lists and vectors are container classes which developers can use to store elements. Vector uses contiguous memory to store elements while list uses non-contiguous memory. The elements in a vector can be accessed randomly but this feature is not available in the list. The elements in a vector can be sorted by using the sort() function while elements in the list have to be sorted manually.

FAQs on List Vs. Vector

1. The elements of which class can be accessed randomly?

The elements of the vector class can be accessed randomly as the vector uses contiguous memory to store the elements. Lists store their elements in non-contiguous memory so their elements cannot be accessed randomly.

2. Can elements in the vector be sorted?

Yes! Elements in a vector can be sorted with the help of the sort() function. No such function is available in the list class so the elements have to be sorted manually.

3. Which class supports iterators?

Iterators are supported by both vector and list classes. In the case of vectors, iterators work in the form of pointers to each element. These elements can be accessed randomly. In the case of lists, the iterators work in the form of pointers to the nodes so the elements cannot be accessed randomly.

4. In which form vectors and lists are implemented?

Vectors are implemented in the form of dynamic arrays as the elements are stored in contiguous memory. Lists are implemented in the form of a doubly linked list in which each element is linked to the previous and the next element.

5. Cache performance of which class is better?

Cache of vector is better as the elements are stored in contiguous memory and each element is stored in the same cache line.

Updated on: 22-Jul-2024

0 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements