C++ std::array cbegin() Function

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

Definition

In C++, std::array::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 <array> to use it.

This is a member of std::array<T, N>, not of a plain C array and not of std::vector. A std::array has a fixed size N that is part of its type, and it lives on the stack like a normal variable.

Syntax

Call cbegin with empty brackets. It takes no arguments.

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

cbegin() works even when the array itself is not const.

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. For std::array<T, 0> it is equal to cend().

C++ Examples

Example 1 — Read-Only Loop

Print all elements without any chance of changing them.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 3> nums = {10, 20, 30};

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

How It Works

  1. nums.cbegin() gives a read-only iterator even though nums is not const.
  2. *it reads each value in turn.
  3. The loop ends at nums.cend().

Example 2 — Add Numbers with std::accumulate

Algorithms that only read are a good match for cbegin().

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

int main() {
    std::array<int, 4> nums = {1, 2, 3, 4};

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

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

How It Works

  1. std::accumulate only reads the values, so read-only iterators are enough.
  2. The third argument 0 is the starting sum.
  3. It adds 1 + 2 + 3 + 4 and returns 10.

Example 3 — Jump Ahead from cbegin()

Array iterators support simple math, so you can jump to any element.

C++
#include <array>
#include <iostream>

int main() {
    std::array<int, 4> nums = {5, 6, 7, 8};

    auto it = nums.cbegin() + 2;

    std::cout << "Third element: " << *it << '\n';
    return 0;
}

How It Works

  1. nums.cbegin() points to the first element, 5.
  2. Adding 2 moves the iterator two places forward, to index 2.
  3. *it reads the value there, which is 7.

Frequently Asked Questions

It returns a const_iterator that points to the first element of the array. You can read the element but not change it.
On a non-const array, begin() gives an iterator that can change elements, while cbegin() always gives a read-only one. On a const array, both are read-only.
It makes your intent clear and lets the compiler stop accidental writes. It is a good habit when a loop should only read.
Yes. Use cbegin() and cend() together so both iterators have the same type.
Yes. It returns an iterator equal to cend(). The loop body simply never runs.

Next: cend()

Get a read-only iterator past the last element with std::array.

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.

10 people found this page helpful