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 <print>
#include <array>
#include <mdspan>
#include "print2d.hpp"
int main()
{
std::array<double, 10> arr{};
// mdspan with rows of 3 values:
std::mdspan mds{arr.data(), arr.size() / 3, 3};
// set values in the mdspan:
for (unsigned i=0; i != mds.extent(0); i++) {
for (unsigned j=0; j != mds.extent(1); j++) {
mds[i, j] = 100 * i + j * 0.1;
}
}
print2D(mds); // print the mdspan
std::println("array: {}", arr); // print the referenced array
}