C++ std::list empty() Function

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

Definition

In C++, std::list::empty() tells you whether a std::list has no elements. It returns true for an empty list and false otherwise.

It does not change the list. Use it before calling front(), back(), pop_front(), or pop_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 empty with empty brackets. It returns a bool.

C++
bool e = l.empty();      // true when there are no elements
if (!l.empty()) {
    l.pop_back();
}
CallWhat it does
l.empty()Returns true if the list has no elements, otherwise false.

l.empty() says the same as l.size() == 0, but it states your intention more clearly, so most programmers prefer it.

Library declarations
C++
bool empty() const noexcept;

Parameters

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

Return Value

Return ValueDescription
booltrue if the list has no elements, false if it has at least one.

C++ Examples

Example 1 — empty() Before and After Adding

A new list is empty. After one push_back(), it is not.

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

int main() {
    std::list<int> l;

    std::cout << std::boolalpha;
    std::cout << "empty: " << l.empty() << '\n';

    l.push_back(5);
    std::cout << "empty: " << l.empty() << '\n';
    return 0;
}

How It Works

  1. A list created with no arguments has no nodes, so empty() is true.
  2. std::boolalpha makes cout print true and false instead of 1 and 0.
  3. After push_back(5) there is one node, so empty() is false.

Example 2 — empty() in a while Loop

Keep taking numbers from the front and add them up until nothing is left.

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

int main() {
    std::list<int> l = {3, 4, 5};
    int total = 0;

    while (!l.empty()) {
        total += l.front();
        l.pop_front();
    }

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

How It Works

  1. !l.empty() is true while there is at least one element to process.
  2. Each pass adds front() to the total and then removes it with pop_front(). The values 3, 4, and 5 are added.
  3. After the third pass the list is empty, the loop stops, and the total is 12.

Example 3 — empty() After splice()

Moving all nodes out of a list leaves it empty. Check both lists.

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

int main() {
    std::list<int> a = {1, 2};
    std::list<int> b = {8, 9};

    a.splice(a.end(), b);

    std::cout << std::boolalpha;
    std::cout << "a empty: " << a.empty() << '\n';
    std::cout << "b empty: " << b.empty() << '\n';
    return 0;
}

How It Works

  1. splice(a.end(), b) moves both nodes of b to the end of a.
  2. List a now has four nodes, so a.empty() is false.
  3. List b gave away every node, so b.empty() is true. The list object b still exists and can be filled again.

Frequently Asked Questions

It returns true if the list has no elements and false otherwise. It does not change the list.
No. empty() only checks. To remove all elements, use clear().
Use empty(). Both give the same answer for std::list, but empty() is clearer and is guaranteed to be fast for every container.
Calling front(), back(), pop_front(), or pop_back() on an empty list is undefined behavior. A quick empty() test avoids crashes.
Yes. It takes constant time. It does not walk through the list.

Next: end()

Get an iterator one place after the last element with std::list.

end() 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