std::span<T,Extent>:: subspan
|
template
<
std::
size_t
Offset,
std::
size_t
Count
=
std::
dynamic_extent
>
|
(1) | (since C++20) |
|
constexpr
std::
span
<
element_type,
std::
dynamic_extent
>
subspan
(
size_type offset,
|
(2) | (since C++20) |
Obtains a subview over some consecutive elements of this span, the elements to be included are determined by an element count and an offset.
- If Count is std:: dynamic_extent , the subview contains all elements starting from the Offset th .
- Otherwise, the subview contains Count elements starting from the Offset th .
? Count
: ( Extent ! = std:: dynamic_extent
? Extent - Offset
: std:: dynamic_extent ) .
|
If Offset <= size ( ) && ( Count == std:: dynamic_extent || Count <= size ( ) - Offset ) is false , the behavior is undefined. |
(until C++26) |
|
If Offset <= size ( ) && ( Count == std:: dynamic_extent || Count <= size ( ) - Offset ) is false :
|
(since C++26) |
- If count is std:: dynamic_extent , the subview contains all elements starting from the offset th .
- Otherwise, the subview contains count elements starting from the offset th .
|
If offset <= size ( ) && ( count == std:: dynamic_extent || count <= size ( ) - offset ) is false , the behavior is undefined. |
(until C++26) |
|
If offset <= size ( ) && ( count == std:: dynamic_extent || count <= size ( ) - offset ) is false :
|
(since C++26) |
Return value
( data ( ) + Offset, Count ! = std:: dynamic_extent ? Count : size ( ) - Offset ) )
( data ( ) + offset, count ! = std:: dynamic_extent ? count : size ( ) - offset ) )
Example
#include <algorithm> #include <cstdio> #include <numeric> #include <ranges> #include <span> void display(std::span<const char> abc) { const auto columns{20U}; const auto rows{abc.size() - columns + 1}; for (auto offset{0U}; offset < rows; ++offset) { std::ranges::for_each(abc.subspan(offset, columns), std::putchar); std::puts(""); } } int main() { char abc[26]; std::ranges::iota(abc, 'A'); display(abc); }
Output:
ABCDEFGHIJKLMNOPQRST BCDEFGHIJKLMNOPQRSTU CDEFGHIJKLMNOPQRSTUV DEFGHIJKLMNOPQRSTUVW EFGHIJKLMNOPQRSTUVWX FGHIJKLMNOPQRSTUVWXY GHIJKLMNOPQRSTUVWXYZ
See also
obtains a subspan consisting of the first
N
elements of the sequence
(public member function) |
|
obtains a subspan consisting of the last
N
elements of the sequence
(public member function) |