std::span<T,Extent>::subspan
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Containers library
(C++17)
(C++11)
(C++26)
(C++26)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++23)
(C++23)
(C++23)
(C++23)
(C++20)
(C++23)
Tables
std::span
(C++26)
(C++23)
(C++23)
(C++23)
(C++23)
span::subspan
template< std::size_t Offset,
(1)
(since C++20)
std::size_t Count = std::dynamic_extent >
constexpr std::span <element_type, /* see below */>
constexpr std::span <element_type, std::dynamic_extent >
(2)
(since C++20)
subspan( size_type offset,
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 Count is std::dynamic_extent , the subview contains all elements starting from the Offsetth.
- Otherwise, the subview contains Count elements starting from the Offsetth.
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 ).
? 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 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:
- If the implementation is hardened, a contract violation occurs. Moreover, if the contract-violation handler returns under "observe" evaluation semantic, the behavior is undefined.
- If the implementation is not hardened, the behavior is undefined.
2) The element count and offset are provided as function arguments, and the subview always has a dynamic extent.
- If count is std::dynamic_extent , the subview contains all elements starting from the offsetth.
- Otherwise, the subview contains count elements starting from the offsetth.
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:
- If the implementation is hardened, a contract violation occurs. Moreover, if the contract-violation handler returns under "observe" evaluation semantic, the behavior is undefined.
- If the implementation is not hardened, the behavior is undefined.
[edit] Return value
1) std::span <element_type, FinalExtent>
(data() + Offset, Count != std::dynamic_extent ? Count : size() - Offset))
(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))
(data() + offset, count != std::dynamic_extent ? count : size() - offset))
[edit] Example
Run this code
#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
[edit] See also
obtains a subspan consisting of the first
(public member function) [edit]
N
elements of the sequence (public member function) [edit]