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.
// raw code
#include <iostream>
#include <array>
#include <mdspan>
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:
for (std::size_t i = 0; i != mds.extent(0); ++i) {
for (std::size_t j = 0; j != mds.extent(1); ++j) {
for (std::size_t k = 0; k != mds.extent(2); ++k) {
std::cout << mds[i, j, k] << ' ';
}
std::cout << '\n';
}
std::cout << '\n';
}
}