C++ std::deque cbegin() Function

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

Definition

In C++, std::deque::cbegin() returns a const_iterator that points to the first element. The letter c stands for const, so you can read through the iterator but not change the element.

It is useful when you want the compiler to protect your data from accidental changes. Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call cbegin with empty brackets. It takes no arguments.

C++
auto it = d.cbegin();
int first = *it;
CallWhat it does
d.cbegin()Returns a read-only iterator to the first element.
*d.cbegin()Reads the first element. Writing to it is a compile error.

cbegin() always gives a const_iterator, even when the deque itself is not const. begin() only does that for a const deque.

Library declarations
C++
const_iterator cbegin() const noexcept;

Parameters

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

Return Value

Return ValueDescription
Const iteratorA const_iterator to the first element. You can read the element and move the iterator, but you cannot assign to the element.
Empty dequeFor an empty deque, cbegin() equals cend().

C++ Examples

Example 1 — Loop with cbegin() and cend()

Print every element without any chance of changing it.

C++
#include <deque>
#include <iostream>

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

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

How It Works

  1. d.cbegin() gives a const_iterator to the first element, 3.
  2. Inside the loop, *it can only be read. A line such as *it = 0; would not compile.
  3. The loop stops when the iterator reaches d.cend().

Example 2 — Jump Ahead with a Const Iterator

A deque iterator is random-access, so you can add a number to it.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<char> letters = {'a', 'b', 'c', 'd', 'e'};

    auto it = letters.cbegin();

    std::cout << "first: " << *it << '\n';
    std::cout << "third: " << *(it + 2) << '\n';
    return 0;
}

How It Works

  1. cbegin() returns an iterator that points to 'a'.
  2. it + 2 moves two places forward without a loop, which lands on 'c'.
  3. This works because a deque supports random access. It is not possible with std::list iterators.

Example 3 — Sum with cbegin() and cend()

Algorithms that only read the data are a good match for const iterators.

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

int main() {
    std::deque<int> prices = {120, 80, 50};

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

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

How It Works

  1. std::accumulate only reads the values, so a read-only iterator pair is the right choice.
  2. cbegin() is the start, cend() is the stop mark, and 0 is the starting sum.
  3. It adds 120 + 80 + 50 and returns 250.

Frequently Asked Questions

It returns a const_iterator that points to the first element of the deque. You can read through it but not modify the element.
On a non-const deque, begin() returns a normal iterator that can change elements. cbegin() always returns a const_iterator, so it is read-only.
Use it when you only read the data, for example when printing or summing. The compiler will then stop you from changing elements by mistake.
Yes. It gives the same result as begin() would on a const deque.
No. std::sort changes the elements, so it needs normal iterators from begin() and end().

Next: cend()

Get a read-only iterator one place after the last element with std::deque.

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.

12 people found this page helpful