Open In App

List in C++ STL

Last Updated : 04 Oct, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A list in C++ is a sequence container that allows you to store elements one after another.

  • Implemented as a doubly linked list and maintains both front and back for fast operations on both the ends.
  • Data is stored in non-contiguous memory, allowing fast insertion and deletion anywhere in the list (beginning, middle, or end).
C++
#include <iostream>
#include <list>
using namespace std;

int main()
{
    // Create a list of integers
    list<int> myList;

    // Add elements to the list
    myList.push_back(10);
    myList.push_back(20);
    myList.push_front(5);

    // Display elements
    cout << "List elements: ";
    for (int n : myList)
    {
        cout << n << " ";
    }
    cout << endl;

    return 0;
}

Output
List elements: 5 10 20 

Syntax

A list is defined as the std::list class template inside the <list> header file.

list<T> l;

where,

  • T: Type of elements in the list.
  • l: Name assigned to the list.

Basic Operations

The basic operations of list are shown below:

1. Inserting Elements

  • insert() is used for fast insertion if the position iterator is known; otherwise, traverse the list to reach the position.
  • push_front() is used to insert at the beginning and push_back() to insert at the end.
  • Time complexity to insert element both at beginning and end : O(1).
C++
#include <iostream>
#include <list>
using namespace std;
int main()
{
    list<int> l = {3, 2};

    // Inserting an element at the end
    l.push_back(5);

    // Inserting an element at the beginning
    l.push_front(1);

    // Inserting an element at a specific position
    auto it = l.begin();
    advance(it, 2);
    l.insert(it, 4);

    for (auto i : l)
        cout << i << " ";
    return 0;
}

Output
1 3 4 2 5 

2. Accessing Elements

  • Lists do not allow random access, so to get an element at a specific position, you need to go through the list one by one from the start or end.
  • The first and last elements can be accessed quickly using front() and back() methods.
  • Time complexity for accessing elements : O(n)
C++
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l = {1, 3, 4, 2, 5};

    // Accessing first and last elements
    cout << l.front() << endl;
    cout << l.back() << endl;

    // Access third element
    cout << *next(l.begin(), 2);

    return 0;
}

Output
1
5
4

3. Updating Elements

  • List elements can be updated by accessing them with an iterator and using the assignment operator (=) to set a new value.
  • Since lists do not support random access, you must use an iterator to reach the element you want to update.\
  • Time complexity for updating: O(1) if you have the iterator, otherwise O(n) to reach the element.
C++
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l = {1, 3, 4, 2, 5};

    // Changing the first element
    l.front() = 11;

    // Move iterator to the second element
    auto it = l.begin();
    advance(it, 2);

    // Update the value using iterator
    *it = 10;

    for (auto i : l)
        cout << i << " ";
    return 0;
}

Output
11 3 10 2 5 

4. Finding Elements

  • To find an element in a list, you can use the find() function from the <algorithm> library.
  • find() returns an iterator to the element if it is found, or the end iterator if it is not found.
  • Time complexity for searching elements : O(n).
C++
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l = {1, 3, 4, 2, 5};

    // Finding 4
    auto it = find(l.begin(), l.end(), 4);

    if (it != l.end())
        cout << *it;
    else
        cout << "Element Not Found!";
    return 0;
}

Output
4

5. Traversing

  • A list can be traversed using begin() and end() iterators in a loop.
  • Start from begin() and keep moving the iterator until it reaches end(), accessing each element along the way.
  • Time complexity for traversal : O(n).
C++
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l = {1, 3, 4, 2, 5};

    // Traversing using iterators
    for (auto it = l.begin(); it != l.end(); ++it)
        cout << *it << " ";
        
    return 0;
}

Output
1 3 4 2 5 

6. Deleting Elements

  • erase() deletes an element from the list using an iterator to its position.
  • pop_front() and pop_back() quickly delete the first and last elements of the list.
  • Time complexity for deletion : O(1)
C++
#include <iostream>
#include <list>
using namespace std;

int main()
{
    list<int> l = {1, 3, 4, 2, 5};

    // Deleting last element
    l.pop_back();

    // Deleting first element
    l.pop_front();

    // Deleting third element
    auto it = l.begin();
    advance(it, 2);
    l.erase(it);

    for (auto i : l)
        cout << i << " ";
    return 0;
}

Output
3 4 

Other Member Functions of List

Following is the list of all member functions of std::list class in C++:

Functions

Description

size()

Returns the number of elements in the list.

empty()

Checks if the list is empty.

rbegin()

Returns a reverse iterator pointing to the last element of the list.

rend()

Returns a reverse iterator pointing to the element before the first element.

clear()

Removes all elements from the list.

List in C++ STL
Visit Course explore course icon

Explore