static const int nCols = Cols_N;

 // конструкторы

 kmatrix() { m = Value_T(); }

 kmatrix(const self& x) { m = x.m; }

 explicit kmatrix(Value_T& x) { m = x.m; }

 // открытые функции

 static int rows() { return Rows_N; }

 static int cols() { return Cols_N; }

 row_type row(int n) { return row_type(begin() * (n * Cols_N)); }

 col_type col(int n) { return col_type(begin() + n); }

 const_row_type row(int n) const {

  return const_row_type(begin() + (n * Cols_N));

 }

 const_col_type col(int n) const {

  return const_col_type(begin() + n);

 }

 iterator begin() { return m.begin(); }

 iterator end() { return m.begin() + size(); }

 const_iterator begin() const { return m; }

 const_iterator end() const { return m + size(); }

 static int size() { return Rows_N * Cols_N; }

 // операторы

 row_type operator[](int n) { return row(n); }

 const_row_type operator[](int n) const { return row(n); }

 // операции присваивания

 self& operator=(const self& x) { m = x.m; return *this; }

 self& operator=(value_type x) { m = x; return *this; }

 self& operator+=(const self& x) { m += x.m; return *this; }

 self& operator-=(const self& x) { m -= x.m; return *this; }

 self& operator+={value_type x) { m += x; return *this; }

 self& operator-=(value_type x) { m -= x; return *this; }

 self& operator*=(value_type x) { m *= x; return *this; }

 self& operator/=(value_type x) { m /= x; return *this; }

 self operator-() { return self(-m); }

 // друзья

 friend self operator+(self x, const self& у) { return x += y; }

 friend self operator-(self x, const self& y) { return x -= y; }

 friend self operator+(self x, value_type y) { return x += y; }

 friend self operator-(self x, value type y) { return x -= y; }

 friend self operator*(self x, value_type y) { return x *= y; }

 friend self operator/(self x, value_type y) { return x /= y; }

 friend bool operator==(const self& x, const self& y) { return x == y; }

 friend bool operator!=(const self& x, const self& y) { return x.m != y.m; }

private:

 kvector<Value_T, (Rows_N + 1) * Cols_N> m;

};

#endif

В примере 11.31 приведена программа, демонстрирующая применение шаблонного класса kmatrix.

Пример 11.31. Применение kmatrix

#include 'kmatrix.hpp'

#include <iostream>

using namespace std;

template<class Iter_T>

void outputRowOrColumn(Iter_T iter, int n) {

 for (int i=0; i < n; ++i) {

  cout << iter[i] << ' ';

 }

 cout << endl;

}

template<class Matrix_T>

void initializeMatrix(Matrix_T& m) {

 int k = 0;

 for (int i=0; i < m.rows(); ++i) {

  for (int j=0; j < m.cols(); ++j) {

   m[i][j] = k++;

  }

 }

}

template<class Matrix_T>

void outputMatrix(Matrix_T& m) {

 for (int i=0; i < m.rows(); ++i) {

  cout << 'Row ' << i << ' = ';

   outputRowOrColumn(m.row(i), m.cols());

 }

 for (int i=0; i < m.cols(); ++i) {

  cout << 'Column ' << i << ' = ';

   outputRowOrColumn(m.col(i), m.rows());

 }

}

int main() {

 kmatrix<int, 2, 4> m;

 initializeMatrix(m); m *= 2;

 outputMatrix(m);

}

Программа примера 11.31 выдает следующий результат.

Row 0 = 0 2 4 6

Row 1 = 8 10 12 14

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату