C++ std::list begin() Function

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

Definition

In C++, std::list::begin() returns an iterator that points at the first element of a std::list. An iterator is like a pointer that you use to walk through the elements.

The iterator of a list is bidirectional. You can move it with ++ and --, but you cannot add a number like it + 2. 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 begin with empty brackets. It is nearly always used together with end().

C++
auto it = l.begin();          // first element
for (auto i = l.begin(); i != l.end(); ++i) {
    // use *i
}
CallWhat it does
l.begin()Returns an iterator to the first element. For a const list it returns a const_iterator.

If the list is empty, begin() is equal to end(), so a loop from begin() to end() does nothing.

Library declarations
C++
iterator begin() noexcept;
const_iterator begin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
iteratorAn iterator that points at the first element. On an empty list it equals end() and must not be dereferenced.

C++ Examples

Example 1 — begin() and end() in a Loop

Walk through a list with an iterator, from the first element to the end.

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

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

    for (auto it = l.begin(); it != l.end(); ++it) {
        std::cout << *it << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. l.begin() gives an iterator that points at the node with 10.
  2. *it reads the element, and ++it follows the link to the next node. The loop stops when it becomes equal to l.end().
  3. The three elements are printed in order. A range-based for loop does exactly this behind the scenes.

Example 2 — begin() Lets You Change Elements

An iterator (not a const one) gives write access to the elements.

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

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

    *l.begin() = 5;

    for (auto it = l.begin(); it != l.end(); ++it) {
        *it *= 2;
    }

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

How It Works

  1. *l.begin() = 5 replaces the first element 10 with 5.
  2. The loop doubles every element through the iterator: 5 becomes 10, 20 becomes 40, and 30 becomes 60.
  3. The changes are made inside the list itself, so the final loop prints 10 40 60.

Example 3 — begin() with std::advance()

A list iterator cannot do it + 2. Use std::advance() to take several steps.

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

int main() {
    std::list<int> l = {10, 20, 30, 40};

    auto it = l.begin();
    std::advance(it, 2);

    std::cout << "third: " << *it << '\n';
    --it;
    std::cout << "back one step: " << *it << '\n';
    return 0;
}

How It Works

  1. it starts at the first node. Writing l.begin() + 2 would not compile for a list.
  2. std::advance(it, 2) uses ++ two times for you, so it now points at the third node, 30.
  3. A list iterator is bidirectional, so --it goes one step back to 20.

Frequently Asked Questions

An iterator that points at the first element of the list. On an empty list it is equal to end().
No. A list iterator only supports ++ and --, one step at a time. Use std::next(l.begin(), 2) or std::advance().
It is an iterator that can move forwards with ++ and backwards with --. It cannot jump many steps at once. std::list and std::set use bidirectional iterators.
begin() returns an iterator, while front() returns a reference to the first element. front() on an empty list is undefined, and begin() is safe to compare with end().
No. Inserting into a std::list never invalidates iterators. Erasing invalidates only the iterators that point at erased elements.

Next: cbegin()

Get a read-only iterator to the first element (C++11) with std::list.

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