When to Use List Instead of Vector in C++?
Last Updated :
04 Apr, 2024
In C++, both std::vector and std::list are sequence containers that can store a collection of elements. However, they have different characteristics and use cases. In this article, we will learn when to use a list instead of a vector in C++.
When to Prefer List Instead of Vector?
Vectors are sequence containers that store the data in the contiguous memory location. This allows us to directly access the element at any position in O(1) time. Whereas, lists are also a sequential container but they store data in non-contiguous memory locations. Here, each element contains the location of the next element in the sequence. To access an element inside the list, we have to go through all the elements before that element leading to the time complexity of O(N).
This difference leads to many cases where it is advantageous to use one container instead of the other. The following are a few such cases where using std::list
in place of std::vector
is more advantageous:
1. Frequent Insertions and Deletions at Both Ends
In std::list, we can efficiently perform insertions and deletions at both ends which makes it a suitable choice when we need a data structure that frequently inserts or deletes elements not only at the end but also at the beginning.
Example:
The below program illustrates the use of a std::list for frequent insertions and deletions at both ends.
C++
// C++ program to illustrate the use of list for
// frequent insertions and deletions at both ends
#include <iostream>
#include <list>
using namespace std;
int main()
{
// Initialize a list of integers
list<int> nums = { 1, 2, 3, 4, 5 };
// Insert at the beginning
nums.push_front(0);
// Delete from the end
nums.pop_back();
// Print the list
for (int num : nums) {
cout << num << " ";
}
cout << endl;
return 0;
}
2. Frequent Insertions and Deletions in the Middle
The std::list allows efficient insertions and deletions in the middle of the list, while std::vector does not because std::vector needs to shift all elements after the insertion or deletion point, which can be costly for large vectors. so, std::list
is a better choice.
Example:
The below program illustrates the use of list for frequent insertions and deletions in the middle.
C++
// C++ program to illustrate the use of list for
// frequent insertions and deletions in the middle
#include <iostream>
#include <list>
using namespace std;
int main()
{
// Initialize a list of integers
list<int> nums = { 1, 2, 3, 4, 5 };
// Insert in the middle
auto it = nums.begin();
advance(it, 2);
nums.insert(it, 0);
// Delete from the middle
it = nums.begin();
advance(it, 2);
nums.erase(it);
// Print the list
for (int num : nums) {
cout << num << " ";
}
cout << endl;
return 0;
}
3. No Random Access Required
The std::list
does not support random access so, if the program does not need to access elements by their index and only requires iterating over the elements, std::list can be a better choice as it provides efficient iteration.
Example:
The below program illustrates the use of list when there is no need for random access.
C++
// C++ program to illustrate the use of list when
// there is no need for random access
#include <iostream>
#include <list>
using namespace std;
int main()
{
// Initialize a list of integers
list<int> nums = { 1, 2, 3, 4, 5 };
// Access elements using an iterator
for (auto it = nums.begin(); it != nums.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
4. Large Elements
If the elements are large, inserting or deleting elements in a std::vector can be costly as it involves copying or moving elements. In such cases, std::list can be a better choice as it only involves updating pointers of the neighboring elements, which is a constant time operation regardless of the size of the elements.
Conclusion
In conclusion, while std::vector is a great general-purpose container in C++, std::list provides several advantages in specific scenarios, especially when frequent insertions and deletions are involved and when there is no need for random access. So, the choice of container should be based on the requirements of the program.
Similar Reads
When to Use Vector Instead of Array in C++?
In C++, both arrays and vectors are used to store collections of data. Arrays are a basic fixed-size sequence of elements, whereas vectors are part of the C++ STL and offer dynamic sizing and more flexibility. There are some cases where using vectors can be more beneficial than using arrays. In this
4 min read
When to Use Deque Instead of Vector in C++?
In C++, both deque and vector are sequence containers that can be used to store collections of elements. However, there are some cases where using deque can be more beneficial than using vector. In this article, we will learn when to use deque instead of vector in C++. When to Prefer Deque Instead o
3 min read
How to Use Iterator with a Vector in C++?
In C++, vectors are the same as dynamic arrays with the ability to resize themselves automatically when an element is inserted or deleted. An iterator is a pointer-like object that can be used to access elements of a container (like an array or a vector). In this article, we will learn how to use an
2 min read
When should I use Vector at() instead of vector operator[]?
In C++, vector provides two methods for accessing its elements using indexes: vector operator[] and vector at(). Although both methods provide similar functionality, but there are some differences between them. The following table lists the primary differences between the vector operator[] and vecto
2 min read
How to Insert into Vector Using Iterator in C++?
In C++, a vector is a dynamic array that can grow and shrink in size as needed. Vectors are sequence containers representing arrays that can change in size. In this article, we will learn how to insert elements into a vector using an iterator in C++. Example: Input: myVector = {10,20,30}; Output: Ve
2 min read
When to Use a Functor Instead of a Function in C++?
In C++, both functors and functions can be used to encapsulate behavior and provide callable objects. Still, there are specific scenarios where using a functor is more advantageous than a regular function. In this article, we will learn when to use a functor instead of a function in C++. When to Pre
4 min read
Set of Vectors in C++ STL with Examples
Set in STL Sets are a type of associative containers in which each element has to be unique, because the value of the element identifies it. The value of the element cannot be modified once it is added to the set, though it is possible to remove and add the modified value of that element. Vector in
2 min read
How to iterate through a Vector without using Iterators in C++
In this article, we will learn how to iterate through the vector without using iterator in C++. The most efficient method to iterate through the vector without using iterator is by using traditional for loop. It accesses all the elements using index starting from 0 to vector size() - 1. Letâs take a
2 min read
When to Use Lambda Expressions Instead of Functions in C++?
In C++, both lambda expressions and functions are used to define operations that can be invoked somewhere else in the code. However, there are some cases where using lambda expressions can be more beneficial than using functions. In this article, we will learn when to use lambda expressions instead
4 min read
How to Initialize a Vector with Default Values in C++?
Initialization is the process of assigning the value to the elements of vector. In this article, we will learn how to initialize the vector with default value in C++. The simplest method to initialize the vector with default value is by using constructor during declaration. Letâs take a look at a si
2 min read