Difference Between Similar Terms and Objects

# Difference Between Vector and List

Vector vs List

Often confusing to the programmers, vectors and lists are sequences used in array holdings in C++ and Java. The two terms hold array addresses but with different methods of holding arrays.
The basic thing we need to know is that an array is a “list” which holds some or all of the data, i.e., integers, floating points, or characters and is defined in brackets “[].”
Actually, vectors and lists act according to the instances. So let’s have a look at these two terms one by one.

Vectors
Vectors are used in array holding and elements accessing. Here you can access any element randomly using the “[]” operator. So it becomes easy to look through all the elements or one specific element with a vector operation. So if you insert any object in the end, in the beginning, or in the middle, then vectors have a plus point because you can access the random address and make changes there. However, vectors are a bit slow as compared to list objects. Vectors are considered as synchronized objects, efficient in random access, and they properly hold the data with a synced list. A vector is picked whenever there is no need to insert or delete in the
middle (list) or from the front.
The number of elements in an array may vary dramatically.
Example:
vector V;
V.insert(V.begin(), 3);
assert(V.size() == 1 && V.capacity() >= 1 && V[0] == 3);

Lists
Lists are “double linked sequences” supporting both forward and backward traversal. The time taken in the insertion and deletion in the beginning, end, and in the middle is constant. Insertion and splicing between linked lists do not invalidate any iteration in the elements. Only removal invalidates the iteration. They are not synchronized so they are not accessible randomly. The ordering of iterations may change according to user, but it does not affect any changes in the elements. They are faster than vectors and are ideal for insertion and deletion in the beginning, middle, and end of the element lists.

Example:
#include
// list class-template definition
….
int main()
{
int array[ 4 ] = { 2, 6, 4, 8 };
std::list< int > values;
std::list< int > otherValues;

Summary:
1. A list is not synchronized while a vector is.
2. Lists have no default size while a vector has a default size of 10.
3. Lists and vectors are both dynamically growing arrays.
4. A list is not thread safe whereas a vector is thread safe.
5. Lists, as they apply only to addition and deletion in the front and rear, are faster while
vectors take more CPU.
6. A vector grows by its size twice while a list decreases to half, i.e., 50 per cent.

Latest posts by Nimisha Kaushik (see all)

Sharing is caring!