Difference between revisions of "cpp/container/span/subspan"
From cppreference.com
m (~) |
(∃:+) |
||
Line 31: | Line 31: | ||
=== Return value === | === Return value === | ||
The requested subspan {{tt|r}}, such that {{c|1=r.data() == this->data() + Offset}}. If {{tt|Count}} is {{tt|std::dynamic_extent}}, {{c|1=r.size() == this->size() - Offset}}; otherwise {{c|1=r.size() == Count}}. | The requested subspan {{tt|r}}, such that {{c|1=r.data() == this->data() + Offset}}. If {{tt|Count}} is {{tt|std::dynamic_extent}}, {{c|1=r.size() == this->size() - Offset}}; otherwise {{c|1=r.size() == Count}}. | ||
+ | |||
+ | ===Example=== | ||
+ | {{example | | ||
+ | | code= | ||
+ | #include <algorithm> | ||
+ | #include <cstdio> | ||
+ | #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), | ||
+ | putchar | ||
+ | ); | ||
+ | putchar('\n'); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | int main() | ||
+ | { | ||
+ | char abc[26]; | ||
+ | std::iota(std::begin(abc), std::end(abc), 'A'); | ||
+ | display(abc); | ||
+ | } | ||
+ | | output= | ||
+ | ABCDEFGHIJKLMNOPQRST | ||
+ | BCDEFGHIJKLMNOPQRSTU | ||
+ | CDEFGHIJKLMNOPQRSTUV | ||
+ | DEFGHIJKLMNOPQRSTUVW | ||
+ | EFGHIJKLMNOPQRSTUVWX | ||
+ | FGHIJKLMNOPQRSTUVWXY | ||
+ | GHIJKLMNOPQRSTUVWXYZ | ||
+ | }} | ||
=== See also === | === See also === |
Revision as of 16:05, 1 June 2020
template< std::size_t Offset, std::size_t Count = std::dynamic_extent > |
(1) | |
constexpr std::span<element_type, std::dynamic_extent> subspan( std::size_t Offset, |
(2) | |
Obtains a span that is a view over the Count
elements of this span starting at offset Offset
. If Count
is std::dynamic_extent
, the number of elements in the subspan is size() - offset
(i.e., it ends at the end of *this
.).
(1) is ill-formed if
-
Offset
is greater thanExtent
, or -
Count
is notstd::dynamic_extent
andCount
is greater thanExtent - Offset
.
The behavior is undefined if either Offset
or Count
is out of range. This happens if
-
Offset
is greater thansize()
, or -
Count
is notstd::dynamic_extent
andCount
is greater thansize() - Offset
.
The extent E
of the span returned by (1) is determined as follows:
- If
Count
is notstd::dynamic_extent
,Count
; - Otherwise, if
Extent
is notstd::dynamic_extent
,Extent - Offset
; - Otherwise,
std::dynamic_extent
.
Return value
The requested subspan r
, such that r.data() == this->data() + Offset. If Count
is std::dynamic_extent
, r.size() == this->size() - Offset; otherwise r.size() == Count.
Example
Run this code
#include <algorithm> #include <cstdio> #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), putchar ); putchar('\n'); } } int main() { char abc[26]; std::iota(std::begin(abc), std::end(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) |