W3cubDocs

/C++

std::span<T,Extent>::end

constexpr iterator end() const noexcept;
(since C++20)

Returns an iterator to the element following the last element of the span.

This element acts as a placeholder; attempting to access it results in undefined behavior.

range-begin-end.svg

Parameters

(none).

Return value

Iterator to the element following the last element.

Complexity

Constant.

Example

#include <iostream>
#include <span>
 
void print(std::span<const int> array)
{
    std::cout << "array = ";
    for (auto it = array.begin(); it != array.end(); ++it)
        std::cout << *it << ' ';
    std::cout << '\n';
}
 
void set_first_element(std::span<int> sp, int new_value)
{
    if (not sp.empty())
    {
        std::cout << "old *begin = " << *sp.begin() << '\n';
        *sp.begin() = new_value;
        std::cout << "new *begin = " << *sp.begin() << '\n';
    }
}
 
int main()
{
    int array[]{1, 3, 4, 5};
    print(array);
    set_first_element(array, 2);
    print(array);
}

Output:

array = 1 3 4 5
old *begin = 1
new *begin = 2
array = 2 3 4 5

See also

(C++20)
returns an iterator to the beginning
(public member function)
(C++11)(C++14)
returns an iterator to the end of a container or array
(function template)

© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
https://en.cppreference.com/w/cpp/container/span/end