std::span<T,Extent>::begin, std::span<T,Extent>::cbegin - cppreference.com

From cppreference.com

constexpriteratorbegin()constnoexcept; (1) (since C++20)constexprconst_iteratorcbegin()constnoexcept; (2) (since C++23)Returns an iterator to the first element of the span.

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

Return value

Iterator to the first element.

Complexity

Constant.

Example

Run this code

#include<iostream>#include<span>voidprint(std::span<constint>array){std::cout<<"array = ";for(autoit=array.begin();it!=array.end();++it)std::cout<<*it<<' ';std::cout<<'\n';}voidset_first_element(std::span<int>sp,intnew_value){if(!sp.empty()){std::cout<<"old *begin = "<<*sp.begin()<<'\n';*sp.begin()=new_value;std::cout<<"new *begin = "<<*sp.begin()<<'\n';}}intmain(){intarray[]{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++23)

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]