Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::strided_slice

      From cppreference.com
      <cpp‎ |container‎ |mdspan
       
       
       
       
      Defined in header<mdspan>
      template<class OffsetType,class ExtentType,class StrideType>
      struct strided_slice;
      (since C++26)

      An instance of every specialization ofstrided_slice is a slice specifier used instd::submdspan to select a subset of elements using a set of regularly spaced indices at a specified dimension instd::mdspan.

      Eachstrided_slice objects is characterized by three data members: the offset indexs.offset, the extents.extent, and the strides.stride.

      Given thats.stride is greater than zero, the number of selected indices denoted byN is determined by1+(s.extent-1)/ s.stride ifs.extent is nonzero, otherwise0. The half-open interval from which indices will be selected is given by[s.offsets.offset+ s.extent). The sequence of selected indices is produced as follows:s.offset, ..., s.offset+(N-1)* s.stride.

      This class template has no base classes or declared members other than those shown below.

      Contents

      [edit]Template parameters

      OffsetType - the type of offset
      ExtentType - the type of extent
      StrideType - the type of stride
      Type requirements
      -
      All template parameters must be unsigned or signed integer types or must satisfyintegral-constant-like

      The program is ill-formed if the type requirement is not satisfied.

      [edit]Member types

      Member type Definition
      offset_typeOffsetType
      extent_typeExtentType
      stride_typeStrideType

      [edit]Data members

      Member name Definition
      offset
      a starting index of typeoffset_type
      (public member object)
      extent
      a value of typeextent_type added to offset used to define the upper bound of indices
      (public member object)
      stride
      an increment value of typestride_type which is equivalent to the distance between two indices
      (public member object)

      All these members are declared with[[no_unique_address]] attribute and have default member initializers where each data member is value initialized.

      [edit]Notes

      Every specialization ofstrided_slice is an aggregate class which allowsaggregate initialization (including designated initialization) of data members (for instance,std::strided_slice{.offset=2, .extent=10, .stride=3}).

      The slice specification ofstrided_slice takes advantage of the data memberextent, as opposed to other slice specifications that useend to indicate the upper bound value. This is because it can directly generate a static extent for subview ofstd::mdspan if bothextent andstride are of types that satisfyintegral-constant-like. This enables efficient extraction of subviews with static extents by mixing compile time values with a runtime value ofoffset.

      [edit]Example

      Run this code
      #include <mdspan>#include <print> template<typename View,typename O=int,typename E=int,typename S=int>    requires(View::extents_type::rank()==1)void print_sliced_view(View v, std::strided_slice<O, E, S> s){using index_type= View::index_type;auto subview=std::submdspan(v, s);constauto& submap= subview.mapping(); std::print("[");bool uses_comma=false;for(index_type i=0; i!= subview.extent(0);++i){if(uses_comma)std::print(", ");std::print("{}", subview[i]);        uses_comma=true;}    uses_comma=false;std::print("] extracted from indices [");for(index_type i=0; i!= subview.extent(0);++i){if(uses_comma)std::print(", ");std::print("{}", submap(i)+ s.offset);        uses_comma=true;}std::println("]");} int main(){staticconstexprchar letters[]{'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};constexprstd::mdspan md(letters,26);    print_sliced_view(md,{.offset=0, .extent=10, .stride=1});    print_sliced_view(md,{.offset=2, .extent=10, .stride=1});    print_sliced_view(md,{.offset=0, .extent=5,  .stride=1});    print_sliced_view(md,{.offset=2, .extent=5,  .stride=1});    print_sliced_view(md,{.offset=0, .extent=10, .stride=2});    print_sliced_view(md,{.offset=2, .extent=10, .stride=3});    print_sliced_view(md,{.offset=0, .extent=15, .stride=5});    print_sliced_view(md,{.offset=6, .extent=15, .stride=5});}

      Output:

      [A, B, C, D, E, F, G, H, I, J] extracted from indices [0, 1, 2, 3, 4, 5, 6, 7, 8, 9][C, D, E, F, G, H, I, J, K, L] extracted from indices [2, 3, 4, 5, 6, 7, 8, 9, 10, 11][A, B, C, D, E] extracted from indices [0, 1, 2, 3, 4][C, D, E, F, G] extracted from indices [2, 3, 4, 5, 6][A, C, E, G, I] extracted from indices [0, 2, 4, 6, 8][C, F, I, L] extracted from indices [2, 5, 8, 11][A, F, K] extracted from indices [0, 5, 10][G, L, Q] extracted from indices [6, 11, 16]

      [edit]See also

      BLAS-like slice of a valarray: starting index, length, stride
      (class)[edit]
      (C++26)
      returns a view of a subset of an existingmdspan
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/mdspan/strided_slice&oldid=173448"

      [8]ページ先頭

      ©2009-2025 Movatter.jp