Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/container/span/subspan"

From cppreference.com
< cpp‎ | container‎ | span
(∃:+)
m (Capitalized 1st letter)
 
(5 intermediate revisions by 4 users not shown)
Line 2: Line 2:
 
{{cpp/container/span/navbar}}
 
{{cpp/container/span/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl | num=1| 1=
+
{{dcl|since=c++20|num=1|1=
 
template< std::size_t Offset,
 
template< std::size_t Offset,
 
           std::size_t Count = std::dynamic_extent >
 
           std::size_t Count = std::dynamic_extent >
constexpr std::span<element_type, E /* see below */> subspan() const;
+
constexpr std::span<element_type, E /* see below */>
 +
    subspan() const;
 
}}
 
}}
{{dcl | num=2|1=
+
{{dcl|since=c++20|num=2|1=
constexpr std::span<element_type, std::dynamic_extent>  
+
constexpr std::span<element_type, std::dynamic_extent>
     subspan( std::size_t Offset,
+
     subspan( size_type Offset,
             std::size_t Count = std::dynamic_extent ) const;
+
             size_type Count = std::dynamic_extent ) const;
 
}}
 
}}
 
{{dcl end}}
 
{{dcl end}}
  
Obtains a span that is a view over the {{tt|Count}} elements of this span starting at offset {{tt|Offset}}. If {{tt|Count}} is {{tt|std::dynamic_extent}}, the number of elements in the subspan is {{tt|size() - offset}} (i.e., it ends at the end of {{tt|*this}}.).
+
Obtains a span that is a view over the {{c|Count}} elements of this span starting at offset {{c|Offset}}. If {{c|Count}} is {{lc|std::dynamic_extent}}, the number of elements in the subspan is {{c|size() - offset}} (i.e., it ends at the end of {{c|*this}}).
  
{{v|1}} is ill-formed if
+
@1@ Is ill-formed if
* {{tt|Offset}} is greater than {{tt|Extent}}, or
+
* {{c|Offset}} is greater than {{c|Extent}}, or
* {{tt|Count}} is not {{tt|std::dynamic_extent}} and {{tt|Count}} is greater than {{tt|Extent - Offset}}.
+
* {{c|Count}} is not {{lc|std::dynamic_extent}} and {{c|Count}} is greater than {{c|Extent - Offset}}.
  
The behavior is undefined if either {{tt|Offset}} or {{tt|Count}} is out of range. This happens if
+
The behavior is undefined if either {{c|Offset}} or {{c|Count}} is out of range. This happens if
* {{tt|Offset}} is greater than {{tt|size()}}, or
+
* {{c|Offset}} is greater than {{lc|size()}}, or
* {{tt|Count}} is not {{tt|std::dynamic_extent}} and {{tt|Count}} is greater than {{tt|size() - Offset}}.
+
* {{c|Count}} is not {{lc|std::dynamic_extent}} and {{c|Count}} is greater than {{c|size() - Offset}}.
  
 
The extent {{tt|E}} of the span returned by {{v|1}} is determined as follows:
 
The extent {{tt|E}} of the span returned by {{v|1}} is determined as follows:
* If {{tt|Count}} is not {{tt|std::dynamic_extent}}, {{tt|Count}};
+
* If {{c|Count}} is not {{lc|std::dynamic_extent}}, {{c|Count}};
* Otherwise, if {{tt|Extent}} is not {{tt|std::dynamic_extent}}, {{tt|Extent - Offset}};
+
* Otherwise, if {{tt|Extent}} is not {{lc|std::dynamic_extent}}, {{c|Extent - Offset}};
* Otherwise, {{tt|std::dynamic_extent}}.
+
* Otherwise, {{lc|std::dynamic_extent}}.
  
=== 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 {{c|Count}} is {{lc|std::dynamic_extent}}, {{c|1=r.size() == this->size() - Offset}}; otherwise {{c|1=r.size() == Count}}.
  
 
===Example===
 
===Example===
{{example |
+
{{example
| code=
+
|code=
 
#include <algorithm>
 
#include <algorithm>
 
#include <cstdio>
 
#include <cstdio>
 +
#include <numeric>
 
#include <ranges>
 
#include <ranges>
 
#include <span>
 
#include <span>
Line 42: Line 44:
 
void display(std::span<const char> abc)
 
void display(std::span<const char> abc)
 
{
 
{
     const auto columns{ 20U };
+
     const auto columns{20U};
     const auto rows{ abc.size() - columns + 1 };
+
     const auto rows{abc.size() - columns + 1};
  
     for (auto offset{ 0U }; offset < rows; ++offset) {
+
     for (auto offset{0U}; offset < rows; ++offset)
         std::ranges::for_each(
+
    {
            abc.subspan(offset, columns),  
+
         std::ranges::for_each(abc.subspan(offset, columns), std::putchar);
            putchar
+
         std::putchar('\n');
        );
+
         putchar('\n');
+
 
     }
 
     }
 
}
 
}
Line 60: Line 60:
 
     display(abc);
 
     display(abc);
 
}
 
}
| output=
+
|output=
 
ABCDEFGHIJKLMNOPQRST
 
ABCDEFGHIJKLMNOPQRST
 
BCDEFGHIJKLMNOPQRSTU
 
BCDEFGHIJKLMNOPQRSTU
Line 70: Line 70:
 
}}
 
}}
  
=== See also ===
+
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/container/span/dsc first}}
+
{{dsc inc|cpp/container/span/dsc first}}
{{dsc inc | cpp/container/span/dsc last}}
+
{{dsc inc|cpp/container/span/dsc last}}
 
{{dsc end}}
 
{{dsc end}}
  
{{langlinks|ja|zh}}
+
{{langlinks|de|es|ja|ru|zh}}

Latest revision as of 04:34, 5 November 2023

 
 
 
 
template< std::size_t Offset,

          std::size_t Count = std::dynamic_extent >
constexpr std::span<element_type, E /* see below */>

    subspan() const;
(1) (since C++20)
constexpr std::span<element_type, std::dynamic_extent>

    subspan( size_type Offset,

             size_type Count = std::dynamic_extent ) const;
(2) (since C++20)

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 than Extent, or
  • Count is not std::dynamic_extent and Count is greater than Extent - Offset.

The behavior is undefined if either Offset or Count is out of range. This happens if

  • Offset is greater than size(), or
  • Count is not std::dynamic_extent and Count is greater than size() - Offset.

The extent E of the span returned by (1) is determined as follows:

  • If Count is not std::dynamic_extent, Count;
  • Otherwise, if Extent is not std::dynamic_extent, Extent - Offset;
  • Otherwise, std::dynamic_extent.

[edit] 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.

[edit] 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::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

[edit] 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]