std::span<T,Extent>::begin, std::span<T,Extent>::cbegin - cppreference.com (original) (raw)
constexpr iterator begin() const noexcept; | (1) | (since C++20) |
---|---|---|
constexpr const_iterator cbegin() const noexcept; | (2) | (since C++23) |
Returns an iterator to the first element of *this.
If *this is empty, the returned iterator will be equal to end().
[edit] Return value
Iterator to the first element.
[edit] Complexity
Constant.
[edit] Example
#include #include void print(std::span 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 sp, int new_value) { if (!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
[edit] See also
| | returns an iterator to the end (public member function) [edit] | | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | | returns an iterator to the beginning of a container or array (function template) [edit] |