Article by Ayman Alheraki on January 11 2026 10:35 AM
std::list with std::vector and std::array in C++std::list, std::vector, and std::array are part of the Standard Template Library (STL) in C++. These containers are used in different ways to store and manage data in memory. Each has its advantages and disadvantages that suit specific needs in terms of performance and efficiency. We will examine the differences among them in terms of data storage, usage, and efficiency, along with examples of std::list functions.
std::list, std::vector, and std::arraystd::list:
It is a doubly linked list that allows easy insertion and deletion of elements, especially at the beginning and end of the list. It is efficient for operations that require frequent insertions or deletions, but it is not efficient for random access of elements.
std::vector:
It is a dynamic array that can grow in size, storing data sequentially and supporting random access to elements via indices. It is considered efficient for operations that require quick access to elements using indices but may be less efficient for frequent middle insertions or deletions.
std::array:
It is a fixed-size array that is initialized upon creation and cannot change size afterward. It is characterized by fast access to elements and a fixed size, making it suitable for applications that do not require size flexibility.
std::list, std::vector, and std::array in Data Storage and Usage| Property | std::list | std::vector | std::array |
|---|---|---|---|
| Storage Method | Linked list with nodes | Dynamic array in memory | Fixed array in memory |
| Element Access | Slow, does not support random access | Fast, supports random access | Fast, supports random access |
| Insertion/Deletion | Fast at both ends | Relatively slow at certain locations | Not supported |
| Size | Flexible (dynamic) | Flexible (dynamic) | Fixed |
| Efficiency | Best for insertions and deletions | Best for random access to data | High efficiency for static applications |
std::list with Explanation and Examplesstd::list contains a set of functions that provide complete control over the elements within the list. Below is a list of the most important functions and their uses.
push_back and push_frontUsed to add elements to the end or the beginning of the list.
push_back: Adds an element at the end of the list.
push_front: Adds an element at the beginning of the list.
int main() { std::list<int> myList; myList.push_back(10); // Add an element to the end myList.push_front(20); // Add an element to the beginning
for (int value : myList) { std::cout << value << " "; } // Output: 20 10}pop_back and pop_frontUsed to remove elements from the end or the beginning of the list.
pop_back: Removes the last element from the list.
pop_front: Removes the first element from the list.
myList.pop_back(); // Remove the last elementmyList.pop_front(); // Remove the first elementinsertAllows inserting an element at a specified position using an iterator.
auto it = myList.begin();myList.insert(it, 30); // Insert an element at the beginning of the listremoveRemoves all elements that match a specified value.
myList.remove(10); // Remove all elements that equal 10sizeReturns the number of elements in the list.
std::cout << "Size: " << myList.size() << std::endl;clearRemoves all elements in the list, making it empty.
myList.clear(); // Remove all elementsemptyReturns true if the list is empty, and false if it contains elements.
if (myList.empty()) { std::cout << "The list is empty." << std::endl;}eraseRemoves an element or a range of elements at a specified position using an iterator or a range of iterators.
auto it = myList.begin();myList.erase(it); // Remove the element at the beginning of the listswapSwaps the contents of the list with another list.
std::list<int> otherList = {40, 50, 60};myList.swap(otherList); // Swap content between the two listsmergeMerges two sorted lists in ascending order.
std::list<int> list1 = {1, 3, 5};std::list<int> list2 = {2, 4, 6};list1.merge(list2);// list1 now contains 1, 2, 3, 4, 5, 6sortSorts the elements of the list in ascending order.
myList.sort(); // Sort the listreverseReverses the order of the elements in the list.
myList.reverse(); // Reverse the order of elements in the liststd::list, std::vector, and std::arrayIn std::vector and std::array, elements can be accessed using indices:
std::vector<int> myVector = {1, 2, 3};std::cout << myVector[1] << std::endl; // Access the second elementIn std::list, random access is not supported, and you must use iterators to access elements sequentially.
In std::list, push_back and push_front are efficient in terms of performance.
In std::vector, push_back can be used, but adding elements in the middle of the array is inefficient.
In std::array, addition is not supported after initialization, as the size is fixed.
std::vector is best for dealing with large amounts of data that require random access.
std::list is better for applications requiring frequent deletions and insertions.
std::array provides high performance but is limited by its fixed size.
Each of std::list, std::vector, and std::array offers different features that suit specific programming needs:
std::list: Suitable when frequent insertion and deletion operations are needed without requiring random access.
std::vector: Ideal for random access with a dynamic data size.
std::array: Offers high performance but is limited by its fixed size, making it suitable for static applications.
By utilizing these containers and their various functions, programmers can take advantage of the power of the STL in C++ to organize data efficiently, determining the optimal container for each use case based on performance and efficiency requirements.