Difference Between Vector and List

Vector: Vector is a type of dynamic array which has the ability to resize automatically after insertion or deletion of elements. The elements in vector are placed in contiguous storage so that they can be accessed and traversed using iterators. Element is inserted at the end of the vector.
Example:

vector v;
v.insert(5);
v.delete();

List: List is a double linked sequence that supports both forward and backward traversal. The time taken in the insertion and deletion in the beginning, end and middle is constant. It has the non-contiguous memory and there is no pre-allocated memory.
Example:

list  l;
l.insert_begin(5);
l.delete_end();

Vector-vs-List

Below is a table of differences between Vector and List:

Vector List
It has contiguous memory. While it has non-contiguous memory.
It is synchronized. While it is not synchronized.
Vector may have a default size. List does not have default size.
In vector, each element only requires the space for itself only. In list, each element requires extra space for the node which holds the element, including pointers to the next and previous elements in the list.
Insertion at the end requires constant time but insertion elsewhere is costly. Insertion is cheap no matter where in the list it occurs.
Vector is thread safe. List is not thread safe.
Deletion at the end of the vector needs constant time but for the rest it is O(n). Deletion is cheap no matter where in the list it occurs.
Random access of elements is possible. Random access of elements is not possible.
Iterators become invalid if elements are added to or removed from the vector. Iterators are valid if elements are added to or removed from the list.
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.