Beispiellösung

Content

#include <iomanip>
#include <iostream>
#include "matrix.hpp"

template<typename Matrix>
void print_matrix(const Matrix& A) {
   for (std::size_t i = 0; i < A.m; ++i) {
      for (std::size_t j = 0; j < A.n; ++j) {
	 std::cout << " " << std::setw(8) << A(i, j);
      }
      std::cout << std::endl;
   }
}

template<typename Matrix, typename Initializer>
void init_matrix(Matrix& A, Initializer&& initializer) {
   for (std::size_t i = 0; i < A.m; ++i) {
      for (std::size_t j = 0; j < A.n; ++j) {
	 A(i, j) = initializer(i, j);
      }
   }
}

int main() {
   Matrix<double> A(7, 8, StorageOrder::ColMajor);
   init_matrix(A, [&A](std::size_t i, std::size_t j) {
      return i * A.n + j;
   });
   std::cout << "A:" << std::endl; print_matrix(A);

   Matrix<float> B(3, 3, StorageOrder::RowMajor);
   init_matrix(B, [&B](std::size_t i, std::size_t j) {
      return j * B.m + i;
   });
   std::cout << std::endl << "B:" << std::endl; print_matrix(B);
}
theon$ g++ -Wall -o init-and-print-matrix init-and-print-matrix.cpp
theon$ ./init-and-print-matrix
A:
        0        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       31
       32       33       34       35       36       37       38       39
       40       41       42       43       44       45       46       47
       48       49       50       51       52       53       54       55

B:
        0        3        6
        1        4        7
        2        5        8
theon$