std::span<T,Extent>::subspan - cppreference.com
From cppreference.com
|
|
(1) | (since C++20) |
|
|
(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.
1) The element count and offset are provided as template arguments, and the subview has a dynamic extent only if both Count and Offset are std::dynamic_extent.
- If
Countisstd::dynamic_extent, the subview contains all elements starting from theOffsetth. - Otherwise, the subview contains
Countelements starting from theOffsetth.
Denote the second template argument of the return type as FinalExtent, it is defined as Count != std::dynamic_extent? Count: (Extent != std::dynamic_extent? Extent - Offset: std::dynamic_extent).
If Offset <= Extent && (Count == std::dynamic_extent || Count <= Extent - Offset) is false, the program is ill-formed.
|
If |
(until C++26) |
|
If
|
(since C++26) |
2) The element count and offset are provided as function arguments, and the subview always has a dynamic extent.
- If
countisstd::dynamic_extent, the subview contains all elements starting from theoffsetth. - Otherwise, the subview contains
countelements starting from theoffsetth.
|
If |
(until C++26) |
|
If
|
(since C++26) |
Return value
1) std::span<element_type, FinalExtent>(data() + Offset, Count != std::dynamic_extent ? Count : size() - Offset))
2) std::span<element_type, std::dynamic_extent>(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) [edit] | |
obtains a subspan consisting of the last N elements of the sequence (public member function) [edit] |