example4_strided_with_mdspan.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
#include <mdspan>

int main() {
    std::vector<int> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
    
    // Create a 2x2 mdspan with strides
    std::mdspan<int, std::extents<std::size_t, 2, 2>, 
                std::layout_stride> matrix(data.data(), 
                std::extents<std::size_t, 2, 2>{}, 
                std::array<std::size_t, 2>{1, 3});
    
    // Access and print elements
    for (std::size_t i = 0; i < matrix.extent(0); ++i) {
        for (std::size_t j = 0; j < matrix.extent(1); ++j) {
            std::cout << matrix[i, j] << " ";
        }
        std::cout << std::endl;
    }
    
    return 0;
}
Back to std_mdspan