C++ std::list cbegin() Function

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

Definition

In C++11 and later, std::list::cbegin() returns a const iterator to the first element of a std::list. The c stands for const.

With this iterator you can read elements but not change them. It works even when the list is not const, which makes your intention clear. 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 cbegin with empty brackets and pair it with cend().

C++
auto it = l.cbegin();     // std::list<int>::const_iterator
int x = *it;              // reading is fine
// *it = 5;               // error: read-only
CallWhat it does
l.cbegin()Returns a const_iterator to the first element. On an empty list it equals cend().

begin() on a non-const list gives a writable iterator. cbegin() always gives a read-only one.

Library declarations
C++
const_iterator cbegin() const noexcept;   // since C++11

Parameters

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

Return Value

Return ValueDescription
const_iteratorA read-only iterator that points at the first element.

C++ Examples

Example 1 — cbegin() and cend() in a Loop

Walk through a list and print every element, without any chance of changing it.

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

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

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

How It Works

  1. l.cbegin() gives a read-only iterator to the node with 3.
  2. *it can be read, and ++it moves to the next node. The loop stops at l.cend().
  3. If you wrote *it = 0 inside the loop, the program would not compile. That is the point of a const iterator.

Example 2 — cbegin() Gives a Const Element

Ask the compiler whether the element reached through the iterator is const.

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

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

    auto w = l.begin();
    auto r = l.cbegin();

    std::cout << std::boolalpha;
    std::cout << "begin() is read-only: "
              << std::is_const<std::remove_reference<decltype(*w)>::type>::value << '\n';
    std::cout << "cbegin() is read-only: "
              << std::is_const<std::remove_reference<decltype(*r)>::type>::value << '\n';
    return 0;
}

How It Works

  1. l is not a const list, so l.begin() returns a normal iterator and l.cbegin() returns a const one.
  2. decltype(*r) is the type you get when you dereference. std::remove_reference removes the &, and std::is_const checks for const.
  3. The first line is false because you may change elements through begin(). The second line is true for cbegin().

Example 3 — cbegin() with std::accumulate()

Pass the read-only iterators to a standard algorithm that only needs to look at the data.

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

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

    int total = std::accumulate(l.cbegin(), l.cend(), 0);

    std::cout << "total: " << total << '\n';
    return 0;
}

How It Works

  1. std::accumulate() adds up all elements between two iterators, starting from 0.
  2. It only reads the elements, so cbegin() and cend() are the right choice.
  3. The sum 10 + 20 + 30 is 60, and the list itself is not changed.

Frequently Asked Questions

A const_iterator that points at the first element. You can read the element but not change it.
begin() on a non-const list gives an iterator that can change elements. cbegin() always gives a const_iterator, which is read-only.
C++11 and later.
Use it when a loop or function only needs to read the list. It documents your intention and stops accidental changes.
Yes. An iterator converts to a const_iterator automatically, but a const_iterator cannot be turned back into a writable iterator without erasing or inserting.

Next: cend()

Get a read-only iterator one place after the last element (C++11) with std::list.

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