std::span<T,Extent>::operator[] - cppreference.com
From cppreference.com
|
|
(since C++20) | |
Returns a reference to the idxth element of the sequence.
|
If |
(until C++26) |
|
If
|
(since C++26) |
Parameters
| idx | - | the index of the element to access |
Return value
data()[idx]
Exceptions
Throws nothing.
Example
#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(const std::span<const int> 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:
See also
| 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] | |
converts a span into a view of its underlying bytes (function template) [edit] |