C++ std::list pop_back() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::list

Definition

In C++, std::list::pop_back() removes the last element of a std::list. The last node is unlinked and destroyed, and the list becomes one element shorter.

It does not give you the removed value. To use the value, read it first with back(). Include <list> to use it.

This is a member of std::list<T>, a doubly linked list. Every element lives in its own node with a link to the previous and the next node, so a list can insert and erase anywhere quickly once you hold an iterator. It has no access by index, so l[i] does not exist.

Syntax

Call pop_back with empty brackets. Check that the list is not empty first.

C++
int last = l.back();   // read first
l.pop_back();          // then remove it
CallWhat it does
l.pop_back()Removes the last element. size() becomes one smaller.

pop_back() is like a stack “pop” that returns nothing. Read with back(), then pop.

Library declarations
C++
void pop_back();

Parameters

ParameterDescription
Nonepop_back() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
Nothingpop_back() returns void. The removed element is destroyed.

C++ Examples

Example 1 — pop_back() Removes the Last Element

Take the last number off a list of three.

C++
#include <iostream>
#include <list>

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

    l.pop_back();

    for (int n : l) {
        std::cout << n << ' ';
    }
    std::cout << "(size " << l.size() << ")\n";
    return 0;
}

How It Works

  1. The list holds 1 2 3. The last node is the one with 3.
  2. pop_back() unlinks that node and destroys it. The nodes before it are not touched.
  3. The loop prints 1 2, and size() shows 2.

Example 2 — pop_back() in a while Loop

Read the last element, then remove it, until the list is empty. The loop is safe because it checks empty().

C++
#include <iostream>
#include <list>

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

    while (!l.empty()) {
        std::cout << "pop " << l.back() << '\n';
        l.pop_back();
    }

    std::cout << "left: " << l.size() << '\n';
    return 0;
}

How It Works

  1. The condition !l.empty() keeps the loop running only while at least one element exists.
  2. Inside the loop, back() reads the last value and pop_back() removes it. The values come out in reverse order: 3 2 1.
  3. When the list is empty the condition is false, the loop stops, and pop_back() is never called on an empty list.

Example 3 — pop_back() Keeps Other Iterators Valid

Hold an iterator to the first element, pop the last one, and use the iterator again.

C++
#include <iostream>
#include <list>

int main() {
    std::list<int> l = {10, 20, 30};
    auto first = l.begin();

    l.pop_back();

    std::cout << "first: " << *first << '\n';
    std::cout << "back: " << l.back() << '\n';
    return 0;
}

How It Works

  1. first points at the node with 10.
  2. pop_back() removes only the node with 30. The other nodes, and the iterators to them, are untouched.
  3. *first still reads 10, and back() now reads 20, the new last element.

Frequently Asked Questions

It removes the last element of the list and destroys it. The list then has one element less. It returns nothing.
No. It returns void. Read the value with back() before you call pop_back().
The behavior is undefined. It may crash or corrupt memory. Check empty() before you call it.
Only the iterator or reference to the removed last element becomes invalid. All other iterators stay valid. The end() iterator also stays valid.
pop_back() always removes the last element and needs no iterator. erase() can remove any element, or a range, but needs an iterator.

Next: pop_front()

Remove the first element of the list with std::list.

pop_front() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful