Home C++ Arrays Functions end() C++ std::array end() Function 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.
for (auto it = nums.begin(); it != nums.end(); ++it) {
// use *it
} Call What 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 iterator end() noexcept;
const_iterator end() const noexcept; Parameters Parameter Description None end() takes no parameters. Call it with empty brackets.
Return Value Return Value Description Iterator An iterator to the position after the last element. On a const array it is a const_iterator.
Important
end() is not an element. Reading it is undefined behavior. Use it only in comparisons such as it != nums.end(), or as the stop argument for algorithms. If an algorithm returns end(), it means "not found".
C++ Examples 1. Count elements 2. With find 3. Last element Example 1 — Count with end() - begin() Subtracting two array iterators gives the number of elements between them.
#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 begin() marks the first element and end() marks the place after the last.The gap between them is one step for every element. 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.
#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;
} Found 15 at index 2
99 not found How It Works std::find checks each element from begin() up to, but not including, end().15 is found, and a - nums.begin() turns the iterator into the index 2.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.
#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;
} Last element: 8
Same as back(): 8 How It Works nums.end() is one place after the data, so it is not safe to read.nums.end() - 1 moves back onto the last real element, and * reads it.back() is the shorter way to get the same value, 8.Frequently Asked Questions What does std::array::end() return? It returns an iterator that points one place after the last element of the array. It is a stop mark, not a real element.
Can I dereference end()? No. *nums.end() is undefined behavior because it points outside the array.
Why does end() point past the last element? It makes loops and algorithms simple. They run while the iterator is not end(), and an empty array just has begin() equal to end().
What does it mean when std::find returns end()? It means the value was not found. Always compare the result with end() before you read it.
What is the difference between end() and back()? back() is the last element itself. end() is an iterator after it. To read the last element through end(), use *(nums.end() - 1).
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion