X Tutup
The Wayback Machine - https://web.archive.org/web/20230522120035/https://en.cppreference.com/w/cpp/container/span/begin
Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | container‎ | span
constexpr iterator begin() const noexcept;

Returns an iterator to the first element of the span.

If the span is empty, the returned iterator will be equal to end().

range-begin-end.svg

Contents

[edit] Parameters

(none)

[edit] Return value

Iterator to the first element.

[edit] Complexity

Constant.


[edit] Example

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

Output:

1 3 4 5 
1
2 3 4 5

[edit] See also

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