Scroll to navigation

std::span::begin(3) C++ Standard Libary std::span::begin(3)

NAME

std::span::begin - std::span::begin

Synopsis


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

Parameters


(none)

Return value


Iterator to the first element.

Complexity


Constant.

Example

// Run this code


#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

See also


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

2022.07.31 http://cppreference.com