List in C++ Standard Template Library (STL)
The C++ Standard Template Library (STL) is a powerful tool that provides programmers with a comprehensive collection of container classes and algorithms. The std::list is a container that allows for quick insertion and removal of elements from any position within the container, but does not support fast random access. It is typically implemented as a doubly-linked list. Unlike std::forward_list, std::list allows for bidirectional iteration but is less space efficient.
Operations such as adding, removing, and moving elements within the list or across multiple lists do not invalidate the iterators or references. Iterators are only invalidated when the corresponding element is deleted.
The list container in C++ provides a flexible way to store and manipulate collections of elements. Unlike vectors or arrays, lists allocate memory for each element individually, allowing for efficient insertion and deletion at any position. This is in contrast to vectors, which store elements in contiguous memory, making them better suited for random access but less efficient for frequent insertions and deletions.
Syntax:
#include<list>
std::list <data-type> name_of_list;
The list container is declared in the <list> header file. The list class is contained within the std namespace, so we can refer to it as std::list or use the using directive (using namespace std;). However, it is generally recommended to use the former method for clarity and to avoid potential naming conflicts.
For creating and initializing a list, follow the example:
#include<iostream>
#include<list>
void main() {
std::list<int> myList { 1, 2, 3, 4, 5 };
std::cout << "Output: ";
for (int element : myList)
std::cout << element << " ";
std::cout << std::endl;
system("pause");
}
// output: 1 2 3 4 5.
The list container provides a variety of member functions to perform operations on the list. Here are some commonly used member functions.
Element access:
- front: access the first element (public member function)
- back: access the last element (public member function)
Iterators:
- begin / cbegin(C++11): returns an iterator to the beginning (public member function)
- end / cend(C++11): returns an iterator to the end (public member function)
- rbegin / crbegin(C++11): returns a reverse iterator to the beginning (public member function)
- rend / crend(C++11): returns a reverse iterator to the end (public member function)
Capacity:
- empty: checks whether the container is empty (public member function)
- size: returns the number of elements (public member function)
- max_size: returns the maximum possible number of elements (public member function)
Modifiers:
- clear: clears the contents (public member function)
- insert: inserts elements (public member function)
- insert_range(C++23): inserts a range of elements (public member function)
- emplace(C++11): constructs element in-place (public member function)
- erase: erases elements (public member function)
- push_back: adds an element to the end (public member function)
- emplace_back(C++11): constructs an element in-place at the end (public member function)
- append_range(C++23): adds a range of elements to the end (public member function)
- pop_back: removes the last element (public member function)
- push_front: inserts an element to the beginning (public member function)
- emplace_front(C++11): constructs an element in-place at the beginning (public member function)
- prepend_range(C++23): adds a range of elements to the beginning (public member function)
- pop_front: removes the first element (public member function)
- resize: changes the number of elements stored (public member function)
- swap: swaps the contents (public member function)
Let’s explore these functions through an example using the link.
#include <iostream>
#include <list>
using namespace std;
void main() {
list<int> myList {1, 2, 3, 4, 5};
cout << "Front: " << myList.front() << endl;
cout << "Back: " << myList.back() << endl;
myList.push_front(0);
myList.push_back(6);
//
//
cout << endl;
}
In addition to the basic operations, the list container offers a range of member functions like erase(), sort(), reverse(), and more. These functions enable us to manipulate and modify the list, such as removing elements, sorting in ascending or descending order, and reversing the order of elements.
Here are some important points to remember about the list container:
- Typically, it is created using a dynamic doubly linked list, allowing traversal in both directions.
- It offers faster insert and delete operations compared to arrays and vectors.
- List container allows only sequential access, and random access to any middle element is not possible.
- It is defined as a template, enabling it to hold any data type.
- By default, the list operates as an unsorted list, meaning that the order of elements is not preserved. However, there are methods available for sorting.
Conclusion
The list container in the C++ STL provides an efficient way to store and manipulate elements in a doubly linked list. Its memory allocation scheme enables fast insertion and deletion at any position. With functions like push_front(), push_back(), pop_front(), and pop_back(), adding, removing, and accessing elements becomes a breeze. The list container also offers additional functions like insert(), size(), begin(), and end() to further enhance its versatility.
For further understanding of the member functions of doubly linked lists, kindly refer to the following GitHub link.
Whether you’re getting ready for your initial job interview or striving to enhance your skills in the constantly changing tech industry, our Medium articles are the gateway to your success. Our high-quality content is designed to accelerate your growth. Join the numerous individuals we’ve already supported, and we’re committed to doing the same for you. Follow for more.