Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

std::end: the iterator past a range

std::end returns a container’s past-the-end iterator or a pointer one element beyond a built-in array.

Version: C++23

Syntax

template<class C>
constexpr auto end(C& c) -> decltype(c.end());

template<class T, std::size_t N>
constexpr T* end(T (&array)[N]) noexcept;

std::end(c) from <iterator> calls c.end() and preserves its exact return type, but C++23 does not mandate this overload to be noexcept. For a built-in array T array[N], a separate overload returns array + N and is noexcept.

The returned position is the exclusive right boundary of [begin, end) and must not be dereferenced. It equals the beginning for an empty range. Its validity and invalidation follow the source object; an iterator that outlives its container is dangling.

Qualified std::end(x) handles types with an end() member and built-in arrays. In generic code, using std::end; end(x); additionally permits a custom free overload to be found by ADL. Do not confuse this function with std::ranges::end, which has separate range and sentinel rules.

Examples

#include <iostream>
#include <iterator>

int main() {
    int values[]{4, 7, 9};
    const auto last = std::end(values);
    std::cout << (last - std::begin(values)) << ' ' << *(last - 1) << '\n';
}

Result

3 9

std::end → 3 9

Sources