Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::span<T,Extent>::subspan

      From cppreference.com
      <cpp‎ |container‎ |span
       
       
       
       
      template<std::size_t Offset,

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

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

          subspan( size_type offset,

                   size_type count=std::dynamic_extent)const;
      (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 bothCount andOffset arestd::dynamic_extent.
      • IfCount isstd::dynamic_extent, the subview contains all elements starting from theOffsetth.
      • Otherwise, the subview containsCount elements starting from theOffsetth.
      Denote the second template argument of the return type asFinalExtent, it is defined asCount!=std::dynamic_extent
         ? Count
         :(Extent!=std::dynamic_extent
             ? Extent- Offset
             :std::dynamic_extent)
      .
      IfOffset<= Extent&&(Count==std::dynamic_extent|| Count<= Extent- Offset) isfalse, the program is ill-formed.

      IfOffset<= size()&&(Count==std::dynamic_extent|| Count<= size()- Offset) isfalse, the behavior is undefined.

      (until C++26)

      IfOffset<= size()&&(Count==std::dynamic_extent|| Count<= size()- Offset) isfalse:

      • If the implementation ishardened, acontract 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.
      (since C++26)


      2) The element count and offset are provided as function arguments, and the subview always has a dynamic extent.
      • Ifcount isstd::dynamic_extent, the subview contains all elements starting from theoffsetth.
      • Otherwise, the subview containscount elements starting from theoffsetth.

      Ifoffset<= size()&&(count==std::dynamic_extent|| count<= size()- offset) isfalse, the behavior is undefined.

      (until C++26)

      Ifoffset<= size()&&(count==std::dynamic_extent|| count<= size()- offset) isfalse:

      • If the implementation ishardened, acontract 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.
      (since C++26)

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

      [edit]Example

      Run this code
      #include <algorithm>#include <cstdio>#include <numeric>#include <ranges>#include <span> void display(std::span<constchar> abc){constauto columns{20U};constauto 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:

      ABCDEFGHIJKLMNOPQRSTBCDEFGHIJKLMNOPQRSTUCDEFGHIJKLMNOPQRSTUVDEFGHIJKLMNOPQRSTUVWEFGHIJKLMNOPQRSTUVWXFGHIJKLMNOPQRSTUVWXYGHIJKLMNOPQRSTUVWXYZ

      [edit]See also

      obtains a subspan consisting of the firstN elements of the sequence
      (public member function)[edit]
      obtains a subspan consisting of the lastN elements of the sequence
      (public member function)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/span/subspan&oldid=182319"

      [8]ページ先頭

      ©2009-2025 Movatter.jp