How to Use STL List Container in C++ STL

Jinku Hu Feb 02, 2024
  1. Use std::list<T> to Declare List Container Object in C++
  2. Use the insert() Function to Insert Elements at Specified Location in the List in C++
  3. Use the swap() Function to Swap Elements of Two Lists in C++
  4. Use the merge() Function to Join Two Sorted Lists Into One
How to Use STL List Container in C++ STL

This article will demonstrate multiple methods about how to use STL list container in C++.

Use std::list<T> to Declare List Container Object in C++

The std::list container is part of the standard template library, and it implements a list data structure that provides constant time insertion/removal of elements from any position. It is usually implemented as a doubly-linked list and supports bidirectional iteration as opposed to std::forward_list. On the downside, std::list is not equipped to have fast random access to elements like std::vector or std::deque have. It provides only two constant time functions, front and back to access the first and the last elements. std::list can be initialized with the given data type and the common initializer list notation as demonstrated in the following example code. push_back and push_front methods can be utilized to add elements to either side of the list.

#include <algorithm>
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename T>
void printList(list<T> l) {
  for (const auto &item : l) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  std::list<int> l1 = {11, 12, 13, 14};

  l1.push_front(15);
  printList(l1);
  l1.push_back(16);
  printList(l1);

  return EXIT_SUCCESS;
}

Output:

15; 11; 12; 13; 14;
15; 11; 12; 13; 14; 16;

Use the insert() Function to Insert Elements at Specified Location in the List in C++

The insert() member function can be used to add elements at a given position. The function has multiple overloads, the first of which takes just two arguments: the iterator and the reference to the object. The given element is inserted before the element that the iterator points to. The next code snippet shows how to find the specific value in the list and then insert the desired element before it.

#include <algorithm>
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename T>
void printList(list<T> l) {
  for (const auto &item : l) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  std::list<int> l1 = {11, 12, 13, 14};

  printList(l1);
  auto iter = std::find(l1.begin(), l1.end(), 13);
  if (iter != l1.end()) {
    l1.insert(iter, 55);
  }
  printList(l1);

  return EXIT_SUCCESS;
}

Output:

11; 12; 13; 14;
11; 12; 55; 13; 14;

Use the swap() Function to Swap Elements of Two Lists in C++

Another useful member function of std::list container is swap() that exchanges the elements of the list object with another list which is passed as the only argument. Note that this operation does not move or copy individual elements and all iterators/references remain valid after the function call.

#include <algorithm>
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename T>
void printList(list<T> l) {
  for (const auto &item : l) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  std::list<int> l1 = {11, 12, 13, 14};
  std::list<int> l2 = {1, 2, 3, 4, 11};

  cout << "l2: ";
  printList(l2);
  l2.swap(l1);
  cout << "l2: ";
  printList(l2);

  return EXIT_SUCCESS;
}

Output:

l2: 1; 2; 3; 4; 11;
l2: 11; 12; 13; 14;

Use the merge() Function to Join Two Sorted Lists Into One

Alternatively, one can join elements of two sorted lists into one using the merge member function. Notice that both lists should be sorted in ascending order. merge takes a reference to the list object, elements of which get merged to the caller object. After the operation, the list object passed as the argument becomes empty. The function orders the elements of the resulting list object in ascending order, as shown in the following code sample.

#include <algorithm>
#include <iostream>
#include <list>

using std::cout;
using std::endl;
using std::list;

template <typename T>
void printList(list<T> l) {
  for (const auto &item : l) {
    cout << item << "; ";
  }
  cout << endl;
}

int main() {
  std::list<int> l1 = {8, 10, 2, 4};
  std::list<int> l2 = {7, 3, 9, 5, 1};

  l2.sort();
  l1.sort();
  cout << "l2: ";
  printList(l2);

  l2.merge(l1);

  cout << "l2: ";
  printList(l2);

  return EXIT_SUCCESS;
}

Output:

l2: 1; 3; 5; 7; 9;
l2: 1; 2; 3; 4; 5; 7; 8; 9; 10;
Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

Related Article - C++ List