C++ std::array cend() Function

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

Definition

In C++, std::array::cend() returns a read-only iterator that points one place after the last element. It does not point to a real element, it only marks the end of the range.

Use it as the stop mark in loops that start with cbegin(). 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 cend with empty brackets. It takes no arguments.

C++
for (auto it = nums.cbegin(); it != nums.cend(); ++it) {
    // read *it
}
CallWhat it does
nums.cend()Returns a read-only iterator just after the last element.
nums.cend() - nums.cbegin()Counts the elements. For an array this is the same as size().

Never write *nums.cend(). It points past the data.

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

Parameters

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

Return Value

Return ValueDescription
Const iteratorA const_iterator to the position after the last element. For std::array<T, 0> it equals cbegin().

C++ Examples

Example 1 — Loop Until cend()

Print every price in a read-only loop.

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

int main() {
    std::array<int, 3> prices = {99, 149, 199};

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

How It Works

  1. The loop starts at cbegin(), which is the first price.
  2. The condition it != prices.cend() keeps going until the end mark.
  3. Each pass prints one price, so you see three lines.

Example 2 — Count Matches with std::count_if

Pass cbegin() and cend() to count items that match a rule.

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

int main() {
    std::array<int, 5> nums = {3, 8, 12, 5, 20};

    int big = std::count_if(nums.cbegin(), nums.cend(),
                            [](int n) { return n > 5; });

    std::cout << "Numbers above 5: " << big << '\n';
    return 0;
}

How It Works

  1. The two iterators tell count_if to look at the whole array.
  2. The small function n > 5 is tested on every element.
  3. 8, 12, and 20 pass the test, so the answer is 3.

Example 3 — Empty Range: cbegin() == cend()

An array with zero elements has nothing to loop over.

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

int main() {
    std::array<int, 0> none;

    if (none.cbegin() == none.cend()) {
        std::cout << "cbegin == cend: yes\n";
    }
    std::cout << "Loop runs: " << (none.cbegin() != none.cend()) << '\n';
    return 0;
}

How It Works

  1. std::array<int, 0> is allowed. It holds no elements at all.
  2. For an empty array the start and end marks are the same place, so == is true.
  3. A loop with != cend() would stop at once. The comparison prints 0, which means false.

Frequently Asked Questions

It returns a const_iterator that points one place after the last element. It is used as the stop mark in loops and algorithms.
No. It does not point to a real element, so *nums.cend() is undefined behavior.
On a non-const array, end() gives an iterator that can change elements and cend() gives a read-only one. Both mark the same position.
Write nums.cend() - nums.cbegin(). For an array this equals nums.size(), which is shorter.
There are no elements between the start and the end, so both iterators mark the same spot.

Next: crbegin()

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

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