mdspan/mdspan3d.cpp

The following code example is taken from the book
C++23 - The Complete Guide by Nicolai M. Josuttis, Leanpub, 2026
The code is licensed under a Creative Commons Attribution 4.0 International License. Creative Commons License

// raw code

#include <array>
#include <mdspan>
#include "print3d.hpp"

int main()
{
  std::array arr{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
                 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30};

  // use parts of arr as three-dimensional array:
  std::mdspan mds{arr.data()+4, 2, 3, 3};

  // print the 3-dimensional mdspan:
  print3D(mds);
}