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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
#include <ostream>
#include <utility>
#include <algorithm>
template <size_t rows, size_t cols, typename T = int>
class Matrix {
public:
Matrix(T matSetValue = T {}) : m_rows(rows), m_cols(cols), m_arr(new T* [m_rows]) {
for (size_t i = 0; i < m_rows; ++i) {
m_arr[i] = new T[m_cols];
std::fill_n(m_arr[i], m_cols, matSetValue);
}
}
~Matrix() {
for (size_t i = 0; i < m_rows; ++i)
delete[] m_arr[i];
delete[] m_arr;
}
Matrix(const Matrix<rows, cols, T>& mat);
inline size_t getNumOfRows() const;
inline size_t getNumOfCols() const;
inline T getCellValue(const size_t& row, const size_t& col) const;
inline void setCellValue(const size_t& row, const size_t& col, int value);
Matrix<rows, cols, T>& operator=(const Matrix<rows, cols, T> matrix);
Matrix<rows, cols, T>& swap(Matrix<rows, cols, T>& mat) {
std::swap(m_arr, mat.m_arr);
std::swap(m_rows, mat.m_rows);
std::swap(m_cols, mat.m_cols);
return *this;
}
private:
size_t m_rows {};
size_t m_cols {};
T** m_arr {};
};
template <size_t rows, size_t cols, typename T>
inline size_t Matrix<rows, cols, T>::getNumOfRows() const
{
return m_rows;
};
template <size_t rows, size_t cols, typename T>
inline size_t Matrix<rows, cols, T>::getNumOfCols() const
{
return m_cols;
};
template <size_t rows, size_t cols, typename T>
inline T Matrix<rows, cols, T>::getCellValue(const size_t& row, const size_t& col) const
{
return m_arr[row][col];
};
template <size_t rows, size_t cols, typename T>
inline void Matrix<rows, cols, T>::setCellValue(const size_t& row, const size_t& col, int value)
{
m_arr[row][col] = value;
};
template <size_t rows, size_t cols, typename T = int>
std::ostream& operator<<(std::ostream& os, const Matrix<rows, cols, T>& matrix)
{
const auto currRows {matrix.getNumOfRows()};
const auto currCols {matrix.getNumOfCols()};
for (size_t i = 0; i < currRows; ++i) {
for (size_t j = 0; j < currCols; ++j)
os << ' ' << matrix.getCellValue(i, j);
os << '\n';
}
return os;
};
template <size_t rows, size_t cols, typename T>
inline Matrix<rows, cols, T>::Matrix(const Matrix<rows, cols, T>& matrix) : m_rows(matrix.m_rows), m_cols(matrix.m_cols), m_arr(new T* [matrix.m_rows]) {
for (size_t i = 0; i < m_rows; ++i) {
m_arr[i] = new T[m_cols];
std::copy_n(matrix.m_arr[i], m_cols, m_arr[i]);
}
}
template <size_t rows, size_t cols, typename T>
inline Matrix<rows, cols, T>& Matrix<rows, cols, T>::operator=(const Matrix<rows, cols, T> matrix)
{
swap(*this, matrix);
return *this;
};
int main()
{
Matrix<4, 4> mat;
std::cout << mat << '\n';
Matrix<4, 4> identity(1);
std::cout << identity << '\n';
Matrix<4, 4> mat4(3);
Matrix<4, 4> res {mat4};
std::cout << res << '\n';
}
|