C++ std::array end() Function

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

Definition

In C++, std::array::end() returns an iterator that points one place after the last element. It does not point to a real element. It is only a stop mark.

Together with begin() it describes the whole array as a range. 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 end with empty brackets. It takes no arguments.

C++
for (auto it = nums.begin(); it != nums.end(); ++it) {
    // use *it
}
CallWhat it does
nums.end()Returns an iterator just after the last element.
*(nums.end() - 1)Reads the last element. Go back one place first, then dereference.

Never write *nums.end(). That spot is outside the array.

Library declarations
C++
iterator       end() noexcept;
const_iterator end() const noexcept;

Parameters

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

Return Value

Return ValueDescription
IteratorAn iterator to the position after the last element. On a const array it is a const_iterator.

C++ Examples

Example 1 — Count with end() - begin()

Subtracting two array iterators gives the number of elements between them.

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

int main() {
    std::array<int, 3> nums = {7, 8, 9};

    auto count = nums.end() - nums.begin();

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

How It Works

  1. begin() marks the first element and end() marks the place after the last.
  2. The gap between them is one step for every element.
  3. The array holds three numbers, so the result is 3.

Example 2 — Search with std::find

std::find returns end() when the value is missing.

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

int main() {
    std::array<int, 6> nums = {4, 8, 15, 16, 23, 42};

    auto a = std::find(nums.begin(), nums.end(), 15);
    if (a != nums.end()) {
        std::cout << "Found 15 at index " << (a - nums.begin()) << '\n';
    }

    auto b = std::find(nums.begin(), nums.end(), 99);
    if (b == nums.end()) {
        std::cout << "99 not found\n";
    }
    return 0;
}

How It Works

  1. std::find checks each element from begin() up to, but not including, end().
  2. 15 is found, and a - nums.begin() turns the iterator into the index 2.
  3. 99 is not in the array, so the search ends at end() and we print "not found".

Example 3 — Reach the Last Element

Step back one place from end() to land on the final element.

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

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

    int last = *(nums.end() - 1);

    std::cout << "Last element: " << last << '\n';
    std::cout << "Same as back(): " << nums.back() << '\n';
    return 0;
}

How It Works

  1. nums.end() is one place after the data, so it is not safe to read.
  2. nums.end() - 1 moves back onto the last real element, and * reads it.
  3. back() is the shorter way to get the same value, 8.

Frequently Asked Questions

It returns an iterator that points one place after the last element of the array. It is a stop mark, not a real element.
No. *nums.end() is undefined behavior because it points outside the array.
It makes loops and algorithms simple. They run while the iterator is not end(), and an empty array just has begin() equal to end().
It means the value was not found. Always compare the result with end() before you read it.
back() is the last element itself. end() is an iterator after it. To read the last element through end(), use *(nums.end() - 1).

Next: fill()

Set every element to the same value with std::array.

fill() 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.

7 people found this page helpful