List in C++ Standard Template Library (STL)

Nitish Singh
4 min readJun 12, 2023

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:

  1. front: access the first element (public member function)
  2. back: access the last element (public member function)

Iterators:

  1. begin / cbegin(C++11): returns an iterator to the beginning (public member function)
  2. end / cend(C++11): returns an iterator to the end (public member function)
  3. rbegin / crbegin(C++11): returns a reverse iterator to the beginning (public member function)
  4. rend / crend(C++11): returns a reverse iterator to the end (public member function)

Capacity:

  1. empty: checks whether the container is empty (public member function)
  2. size: returns the number of elements (public member function)
  3. max_size: returns the maximum possible number of elements (public member function)

Modifiers:

  1. clear: clears the contents (public member function)
  2. insert: inserts elements (public member function)
  3. insert_range(C++23): inserts a range of elements (public member function)
  4. emplace(C++11): constructs element in-place (public member function)
  5. erase: erases elements (public member function)
  6. push_back: adds an element to the end (public member function)
  7. emplace_back(C++11): constructs an element in-place at the end (public member function)
  8. append_range(C++23): adds a range of elements to the end (public member function)
  9. pop_back: removes the last element (public member function)
  10. push_front: inserts an element to the beginning (public member function)
  11. emplace_front(C++11): constructs an element in-place at the beginning (public member function)
  12. prepend_range(C++23): adds a range of elements to the beginning (public member function)
  13. pop_front: removes the first element (public member function)
  14. resize: changes the number of elements stored (public member function)
  15. 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:

  1. Typically, it is created using a dynamic doubly linked list, allowing traversal in both directions.
  2. It offers faster insert and delete operations compared to arrays and vectors.
  3. List container allows only sequential access, and random access to any middle element is not possible.
  4. It is defined as a template, enabling it to hold any data type.
  5. 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.

--

--

Nitish Singh

Passionate about empowering devs with practical tips, using modern C++ as the tool, and up-to-date knowledge. Join me on Medium for engaging articles.