std::span::subspan(3)
π NAME π
You're reading the std::span::subspan(3) page from the C++ Standard Libary manual, bestie. The name is literally std::span::subspan β as in std::span::subspan - std::span::subspan. That's it. That's the name. Slay.
β¨ Synopsis β¨
Okay so here's the signature tea, don't spill it:
template< std::size_t Offset,
std::size_t Count = std::dynamic_extent > (1)
(since C++20)
constexpr std::span<element_type, E /* see below */>
subspan() const;
constexpr std::span<element_type, std::dynamic_extent>
subspan( size_type Offset, (2)
(since C++20)
size_type Count = std::dynamic_extent ) const;
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).
Overload (1) is ill-formed if:
Offsetis greater thanExtent, orCountis notstd::dynamic_extentandCountis greater thanExtent - Offset.
β οΈ The behavior is undefined if eitherOffsetorCountis out of range. This happens if: -Offsetis greater thansize(), or -Countis notstd::dynamic_extentandCountis greater thansize() - Offset.
The extent E of the span returned by (1) is determined as follows:
- If
Countis notstd::dynamic_extent,Count; - Otherwise, if
Extentis 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, bestie:
// 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::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 π
firstβ obtains a subspan consisting of the first N elements of the sequence (public member function)lastβ obtains a subspan consisting of the last N elements of the sequence (public member function)
Source/version cuties: http://cppreference.com, dated 2024.06.10. That's the whole page, bestie β go be iconic with your subspan π
β¨π¦
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms