C++ std::list size() Function

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

Definition

In C++, std::list::size() returns the number of elements that a std::list holds right now. An empty list has size 0.

The value changes when you add or remove elements. Since C++11 the list keeps the count, so size() is fast. 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 size with empty brackets. It returns an unsigned number.

C++
std::size_t n = l.size();     // how many elements
bool has = l.size() > 0;
CallWhat it does
l.size()Returns the count of elements as a size_type, which is an unsigned integer type.

To test for “no elements”, l.empty() is clearer than l.size() == 0.

Library declarations
C++
size_type size() const noexcept;

Parameters

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

Return Value

Return ValueDescription
size_typeThe number of elements in the list. It is unsigned, usually std::size_t.

C++ Examples

Example 1 — size() Counts the Elements

Print the size of a list that you create with five zeros.

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

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

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

How It Works

  1. std::list<int> l(5, 0) creates a list with five nodes, each holding 0.
  2. size() returns 5, the number of nodes in the list.
  3. front() reads the first element, which is one of the five zeros.

Example 2 — size() After Each Change

Watch the size grow and shrink as you push, insert, and pop.

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

int main() {
    std::list<int> l;
    std::cout << "start: " << l.size() << '\n';

    l.push_back(1);
    l.push_back(3);
    std::cout << "after push_back x2: " << l.size() << '\n';

    l.insert(std::next(l.begin()), 2);
    std::cout << "after insert: " << l.size() << '\n';

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

How It Works

  1. A new list has size 0. Two calls to push_back() raise it to 2.
  2. insert() adds one more node in the middle, so the size becomes 3.
  3. pop_front() removes the first node, so the size goes back to 2. The list always keeps the count up to date.

Example 3 — size() Compared with std::distance()

Counting by walking through the list gives the same number, but it is slower.

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

int main() {
    std::list<int> l = {5, 6, 7, 8};

    std::cout << "size: " << l.size() << '\n';
    std::cout << "distance: " << std::distance(l.begin(), l.end()) << '\n';
    return 0;
}

How It Works

  1. size() reads the count that the list stores, so it needs no walk through the nodes.
  2. std::distance(l.begin(), l.end()) has to step through all four nodes one by one to count them.
  3. Both give 4. Prefer size(), because it takes the same time for a list of ten or ten million elements.

Frequently Asked Questions

The number of elements currently stored in the list. An empty list returns 0.
Yes since C++11, which requires size() to be constant time for every container. Before C++11 some implementations counted the nodes on each call.
No. Each element has its own node, so there is no spare space to report. Only std::vector and std::string have capacity().
size() returns an unsigned type. On an empty list, 0 - 1 wraps around to the biggest unsigned value. Check empty() before you subtract.
size() is how many elements the list holds now. max_size() is the biggest number the library could ever hold on your system.

Next: sort()

Sort the elements of the list (stable) with std::list.

sort() 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.

11 people found this page helpful