(C++17) | ||||
| Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
| Associative | ||||
| Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
| Adaptors | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Views | ||||
(C++20) | ||||
(C++23) | ||||
| Tables | ||||
| Iterator invalidation | ||||
| Member function table | ||||
| Non-member function table |
| Member functions | ||||
| Element access | ||||
(C++26) | ||||
span::operator[] | ||||
| Iterators | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
| Observers | ||||
| Subviews | ||||
| Non-member functions | ||||
| Non-member constant | ||||
| Deduction guides |
constexpr reference operator[]( size_type idx)const; | (since C++20) | |
Returns a reference to theidxth element of the sequence.
Ifidx< size() isfalse, the behavior is undefined. | (until C++26) |
Ifidx< size() isfalse:
| (since C++26) |
Contents |
| idx | - | the index of the element to access |
data()[idx]
Throws nothing.
#include <cstddef>#include <iostream>#include <span>#include <utility> void reverse(std::span<int> span){for(std::size_t i=0, j=std::size(span); i< j;++i){--j;std::swap(span[i], span[j]);}} void print(conststd::span<constint> span){for(int element: span)std::cout<< element<<' ';std::cout<<'\n';} int main(){int data[]{1,2,3,4,5}; print(data); reverse(data); print(data);}
Output:
1 2 3 4 55 4 3 2 1
(C++26) | access specified element with bounds checking (public member function)[edit] |
| direct access to the underlying contiguous storage (public member function)[edit] | |
| returns the number of elements (public member function)[edit] | |
(C++20) | converts aspan into a view of its underlying bytes(function template)[edit] |