Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::mdspan

      From cppreference.com
      <cpp‎ |container
       
       
       
       
      Defined in header<mdspan>
      template<

         class T,
         class Extents,
         class LayoutPolicy=std::layout_right,
         class AccessorPolicy=std::default_accessor<T>

      >class mdspan;
      (since C++23)

      std::mdspan is a multidimensional array view that maps a multidimensional index to an element of the array. The mapping and element access policies are configurable, and the underlying array need not be contiguous or even exist in memory at all.

      Each specializationMDS ofmdspan modelscopyable and satisfies:

      A specialization ofmdspan is aTriviallyCopyable type if itsaccessor_type,mapping_type anddata_handle_type areTriviallyCopyable types.

      Contents

      [edit]Template parameters

      T - element type; a complete object type that is neither an abstract class type nor an array type.
      Extents - specifies number of dimensions, their sizes, and which are known at compile time. Must be a specialization ofstd::extents.
      LayoutPolicy - specifies how to convert multidimensional index to underlying 1D index (column-major 3D array, symmetric triangular 2D matrix, etc). Must satisfy the requirements ofLayoutMappingPolicy.
      AccessorPolicy - specifies how to convert underlying 1D index to a reference to T. Must satisfy the constraint thatstd::is_same_v<T,typename AccessorPolicy​::​element_type> istrue. Must satisfy the requirements ofAccessorPolicy.

      [edit]Member types

      Member Definition
      extents_typeExtents
      layout_typeLayoutPolicy
      accessor_typeAccessorPolicy
      mapping_typeLayoutPolicy::mapping<Extents>
      element_typeT
      value_typestd::remove_cv_t<T>
      index_typeExtents::index_type
      size_typeExtents::size_type
      rank_typeExtents::rank_type
      data_handle_typeAccessorPolicy::data_handle_type
      referenceAccessorPolicy::reference

      [edit]Data members

      Member Description
      accessor_typeacc_(private) the accessor
      (exposition-only member object*)
      mapping_typemap_(private) the layout mapping
      (exposition-only member object*)
      data_handle_typeptr_(private) the underlying data handle
      (exposition-only member object*)

      [edit]Member functions

      constructs anmdspan
      (public member function)[edit]
      assigns anmdspan
      (public member function)[edit]
      Element access
      accesses an element at the specified multidimensional index
      (public member function)[edit]
      Observers
      [static]
      returns the rank of amdspan
      (public static member function)[edit]
      [static]
      returns the dynamic rank of amdspan
      (public static member function)[edit]
      returns the static extent size of amdspan at a given rank index
      (public static member function)[edit]
      returns the extent of amdspan at a given rank index
      (public member function)[edit]
      returns the size of the multidimensional index space
      (public member function)[edit]
      checks if the size of the index space is zero
      (public member function)[edit]
      obtains the stride along the specified dimension
      (public member function)[edit]
      obtains the extents object
      (public member function)[edit]
      obtains the pointer to the underlying 1D sequence
      (public member function)[edit]
      obtains the mapping object
      (public member function)[edit]
      obtains the accessor policy object
      (public member function)[edit]
      determines if this mdspan's mapping is unique (every combination of indices maps to a different underlying element)
      (public member function)[edit]
      determines if this mdspan's mapping is exhaustive (every underlying element can be accessed with some combination of indices)
      (public member function)[edit]
      determines if this mdspan's mapping is strided (in each dimension, incrementing an index jumps over the same number of underlying elements every time)
      (public member function)[edit]
      determines if this mdspan's layout mapping is always unique
      (public static member function)[edit]
      determines if this mdspan's layout mapping is always exhaustive
      (public static member function)[edit]
      determines if this mdspan's layout mapping is always strided
      (public static member function)[edit]

      [edit]Non-member functions

      specializes thestd::swap algorithm for mdspan
      (function template)[edit]
      Subviews
      (C++26)
      returns a view of a subset of an existingmdspan
      (function template)[edit]
      creates new extents from the existing extents and slice specifiers
      (function template)[edit]

      [edit]Helper types and templates

      (C++23)
      a descriptor of a multidimensional index space of some rank
      (class template)[edit]
      (C++23)(C++26)
      convenience alias template for an all-dynamicstd::extents
      (alias template)[edit]
      a type for indexed access to elements ofmdspan
      (class template)[edit]
      a type for aligned access to elements ofmdspan
      (class template)[edit]
      Layout mapping policies
      column-major multidimensional array layout mapping policy; leftmost extent has stride1
      (class)[edit]
      row-major multidimensional array layout mapping policy; rightmost extent has stride1
      (class)[edit]
      a layout mapping policy with user-defined strides
      (class)[edit]
      column-major layout mapping policy with padding stride that can be greater than or equal to the leftmost extent
      (class template)[edit]
      row-major layout mapping policy with padding stride that can be greater than or equal to the rightmost extent
      (class template)[edit]
      Subviews helpers
      a slice specifier tag describing full range of indices in the specified extent.
      (tag)[edit]
      a slice specifier representing a set of regularly spaced indices as indicated by an offset, an extent, and a stride
      (class template)[edit]
      a return type of the overloads ofsubmdspan_mapping
      (class template)[edit]

      [edit]Deduction guides

      [edit]Notes

      Feature-test macroValueStdFeature
      __cpp_lib_mdspan202207L(C++23)std::mdspan
      __cpp_lib_submdspan202306L(C++26)std::submdspan
      202403L(C++26)std::mdspan padded layouts
      __cpp_lib_aligned_accessor202411L(C++26)std::aligned_accessor

      [edit]Example

      Can be previewed onCompiler Explorer.

      Run this code
      #include <cstddef>#include <mdspan>#include <print>#include <vector> int main(){std::vector v{1,2,3,4,5,6,7,8,9,10,11,12}; // View data as contiguous memory representing 2 rows of 6 ints eachauto ms2= std::mdspan(v.data(),2,6);// View the same data as a 3D array 2 x 3 x 2auto ms3= std::mdspan(v.data(),2,3,2); // Write data using 2D viewfor(std::size_t i=0; i!= ms2.extent(0); i++)for(std::size_t j=0; j!= ms2.extent(1); j++)            ms2[i, j]= i*1000+ j; // Read back using 3D viewfor(std::size_t i=0; i!= ms3.extent(0); i++){std::println("slice @ i = {}", i);for(std::size_t j=0; j!= ms3.extent(1); j++){for(std::size_t k=0; k!= ms3.extent(2); k++)std::print("{} ", ms3[i, j, k]);std::println("");}}}

      Output:

      slice @ i = 00 12 34 5slice @ i = 11000 10011002 10031004 1005

      [edit]See also

      (C++20)
      a non-owning view over a contiguous sequence of objects
      (class template)[edit]
      numeric arrays, array masks and array slices
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/mdspan&oldid=180236"

      [8]ページ先頭

      ©2009-2025 Movatter.jp