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.
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.
list l; l.insert_begin(5); l.delete_end();
Below is a table of differences between Vector and 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.|