C++ list vs vector – Difference Between Vector and List in C++ | Vector vs List | Examples of std::vector & std::list

Difference Between Vector and List in CPP: In the previous article, we have discussed How to check if a given key exists in a Map | C++. Let us learn the Difference Between Vector and List in C++ Program.

Vector vs List in the C++

C++ vector vs list: In this article, we are going to learn about the difference between vector and list in C++.

Before going to this difference directly, First of all, we have to know what is ‘STL’? STL stands for Standard Template Library it is a library of headers containing commonly used data structures and algorithms. They are defined using C++ templates, you can also use different data types to define this.

This STP is broadly divided into four sections:

  1. Containers
  2. Iterators
  3. Algorithms
  4. Functional

C++ list vs vector: Vectors and Lists are the most used containers. Both store a no of items, just like an array but there are interesting differences between the two.

1. Vector:

Vector vs list c++: Vectors are sequence containers means it contains all the elements in a contiguous memory representing arrays that can change in size. Just like arrays, vectors use contiguous storage location for their elements, which means that their element can also be accessed using offsets on regular pointers to its elements, and just as efficiently in arrays.

Pros: Vector is dynamic. It is fast to add items to the end and fast to remove items from the middle or beginning and we can use[].vector is thread-safe and it is synchronized also.

Cons: but it is slow at adding/removing items from the middle or beginning.

Vectors are also called dynamic arrays i.e. you can add and remove items at run-time.

Vectors are perfect for :

  • When you need to access items arbitrarily with []brackets.
  • When you only need to add items to the end of the list.

Insertion & Deletion :

  • Insertion and deletion in the vector are less efficient than a list.
  • As insertion or deletion at start or middle will make all elements to shift by one.`

2. List:

Vector vs list: Lists are sequence containers that allow non-contiguous memory allocation so constant time insert and delete operations anywhere within the sequence and iteration in both directions are possible easily.

Pros: Lists are also dynamic, fast to add items to the beginning, middle, and end so long as you have an iterator. Traverse forward and Backward. Lists are not thread-safe and it is not synchronized.

Cons: slow at accessing items from the middle and no use of [].

The list usually refers to a linear collection of data. one form of linear list is an array, mainly lists are doubly linked lists. They allow fast addition of items to the beginning, middle, and end.

Lists are perfect for:

  • When you only need to access items in the order, either forwards or reverse.
  • When you have to add items to the middle or beginning of the list often very quickly.

Insertion & Deletion of the List:

  • Insertion and Deletion in the list are more efficient than a vector.
  • As insertion or deletion at the start, middle, or at the end only causes swapping of a couple of pointers internally.

Top 10 differences in Vector and List of the C++

Linked list vs vector c++: Here are some of the top points related to Vector vs List of the C++:

  • A Vector has contiguous memory
  • A list has noncontiguous memory
  • A list is not synchronized while a vector is
  • Lists and vectors are both dynamically growing arrays
  • A Vector Pre allocates space for future
  • A list doesn’t pre-allocate space for future
  • Lists have no default size while a vector has a default size of 10
  • A list is not thread-safe whereas a vector is thread-safe
  • A vector grows by its size twice while a list decreases to half, i.e., 50 percent
  • Lists, as they apply only to addition and deletion in the front and rear, are faster while vectors take more CPU

std::vector vs std::list | Vector and List in the C++

C++ list vs vector: The elements of Std List stores at non-contiguous memory locations i.e. it internally uses a doubly linked list i.e.

vector list difference

Whereas, a vector stores elements at contiguous memory locations like an array i.e.

vector vs list

Examples of Vector List in C++

Let us discuss examples of C++ vector vs list.

Example #1 – Vector Example

#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
for (int i = 5; i <= 15; i++)
{
vec.push_back(i);
}
cout<< "Size of vector is: " << vec.size() << endl;
cout << "Vector elements are given below : ";
for (auto i = vec.begin(); i != vec.end(); i++)
cout << *i << endl;
vec.insert(vec.begin(), 20);
cout<< "Size of vector is: " << vec.size() << endl;
cout << "Updated Vector elements are given below : ";
for (auto i = vec.begin(); i != vec.end(); i++)
cout << *i << endl;
return 0;
}

Output: 

cpp vector example output

Example #2 – List Example

#include <list>
#include <iterator>
using namespace std;
//function to display the elements of list using iterator 'itr'
void display(list <int> x)
{
list <int> :: iterator itr;
for(itr = x.begin(); itr != x.end(); itr++)
cout << *itr << endl;
}
int main()
{
list <int> list1, list2;
for (int i = 0; i < 5; ++i)
{
list1.push_back(i * 10);
list2.push_front(i + 4);
}
cout << "Elements in List 1 are: "<< endl;
display(list1);
cout << "Elements in List 2 are : " << endl;
display(list2);
cout << "Reversing the list2 : "<< endl;
list2.reverse();
display(list2);
cout << "Sorting the elements of list2:"<< endl;
list2.sort();
display(list2);
return 0;