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
Call
What 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
Parameter
Description
None
cbegin() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const_iterator
A 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;
}
Output
3 6 9
How It Works
l.cbegin() gives a read-only iterator to the node with 3.
*it can be read, and ++it moves to the next node. The loop stops at l.cend().
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;
}
Output
begin() is read-only: false
cbegin() is read-only: true
How It Works
l is not a const list, so l.begin() returns a normal iterator and l.cbegin() returns a const one.
decltype(*r) is the type you get when you dereference. std::remove_reference removes the &, and std::is_const checks for const.
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;
}
Output
total: 60
How It Works
std::accumulate() adds up all elements between two iterators, starting from 0.
It only reads the elements, so cbegin() and cend() are the right choice.
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.