From cppreference.com
constexprreferenceoperator[](size_typeidx)const;(since C++20)Returns a reference to the idxth element of the sequence.
If idx<size() is false, the behavior is undefined.
(until C++26)If idx<size() is false:
If the implementation is
, a
occurs.
If the implementation is not hardened, the behavior is undefined.
(since C++26)Parameters
idx - the index of the element to access Return value
data()[idx]
Exceptions
Throws nothing.
Example
Run this code
#include<cstddef>#include<iostream>#include<span>#include<utility>voidreverse(std::span<int>span){for(std::size_ti=0,j=std::size(span);i<j;++i){--j;std::swap(span[i],span[j]);}}voidprint(conststd::span<constint>span){for(intelement:span)std::cout<<element<<' ';std::cout<<'\n';}intmain(){intdata[]{1,2,3,4,5};print(data);reverse(data);print(data);}Output:
1 2 3 4 5 5 4 3 2 1 See also
(C++26)
access specified element with bounds checking
(public member function)
direct access to the underlying contiguous storage
(public member function)
returns the number of elements
(public member function)
(C++20)
converts a span into a view of its underlying bytes
(function template)