Advertisement
Amonin

Matrix Iter

Dec 15th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4.  
  5. using std::cin;
  6. using std::cout;
  7. using std::vector;
  8. using std::ostream;
  9. using std::string;
  10. using std::pair;
  11. using std::make_pair;
  12.  
  13. template <typename T>
  14. class Matrix {
  15. private:
  16.     vector<vector<T>> body;
  17.  
  18. public:
  19.     Matrix(const vector<vector<T>>& d)
  20.             : body(d) {
  21.     }
  22.     //  размер
  23.     pair<size_t, size_t> size() const {
  24.         pair<size_t, size_t> a = make_pair(body.size(), body.front().size());
  25.         return a;
  26.     }
  27.     T& operator() (size_t i, size_t j) {
  28.         return body[i][j];
  29.     }
  30.     const T& operator() (size_t i, size_t j) const {
  31.         return body[i][j];
  32.     }
  33.     //сложение и скаляр
  34.     Matrix& operator += (const Matrix& other) {
  35.         for (size_t i = 0; i != body.size(); ++i)
  36.             for (size_t j = 0; j != body[i].size(); ++j)
  37.                 body[i][j] += other.body[i][j];
  38.         return *this;
  39.     }
  40.     Matrix operator + (const Matrix& other) const {
  41.         auto C = *this;
  42.         C += other;
  43.         return C;
  44.     }
  45.     template <typename N>
  46.     Matrix& operator *= (const N& scalar) {
  47.         for (size_t i = 0; i != body.size(); ++i) {
  48.             for (size_t j = 0; j != body[i].size(); ++j) {
  49.                 body[i][j] *= scalar;
  50.             }
  51.         }
  52.         return *this;
  53.     }
  54.     template <typename N>
  55.     Matrix operator * (const N& scalar) const {
  56.         auto C = *this;
  57.         C *= scalar;
  58.         return C;
  59.     }
  60.     //транспозиция
  61.     Matrix& transpose() {
  62.         size_t n = body.size();
  63.         size_t m = body.front().size();
  64.         vector<vector<T>> matT(m, vector<T>(n));
  65.         for (size_t i = 0; i != n; ++i) {
  66.             for (size_t j = 0; j != m; ++j) {
  67.                 matT[j][i] = body[i][j];
  68.             }
  69.         }
  70.         body = matT;
  71.         return *this;
  72.     }
  73.     Matrix transposed() const {
  74.         Matrix mat = *this;
  75.         return mat.transpose();
  76.     }
  77.     //перемножение
  78.     Matrix& operator *= (const Matrix& other) {
  79.         size_t m = body.size();
  80.         size_t n = body.front().size();
  81.         size_t p = other.size().second;
  82.         std::vector<std::vector<T>> prod(m, std::vector<T>(p, 0));
  83.         for (size_t i = 0; i != m; ++i) {
  84.             for (size_t j = 0; j != p; ++j) {
  85.                 for (size_t k = 0; k != n; ++k) {
  86.                     prod[i][j] += body[i][k] * other.body[k][j];
  87.                 }
  88.             }
  89.         }
  90.         body = prod;
  91.         return *this;
  92.     }
  93.     Matrix operator * (const Matrix& other) const {
  94.         Matrix prod = *this;
  95.         prod *= other;
  96.         return prod;
  97.     }
  98.     //итераторы
  99.     class Iter {
  100.     private:
  101.         size_t row, col;
  102.         Matrix<T> mat;
  103.  
  104.     public:
  105.         Iter(const size_t i, const size_t j, const Matrix<T>& a)
  106.                 : row(i)
  107.                 , col(j)
  108.                 , mat(a) {
  109.         }
  110.         T operator * () const {
  111.             return mat(row, col);
  112.         }
  113.         Iter& operator++ () {
  114.             ++col;
  115.             if (col >= mat.size().second) {
  116.                 ++row;
  117.                 col = 0;
  118.             }
  119.             return *this;
  120.         }
  121.         bool operator == (const Iter& other) const {
  122.             return row == other.row && col == other.col;
  123.         }
  124.         bool operator != (const Iter& other) const {
  125.             return !(*this == other);
  126.         }
  127.     };
  128.     Iter begin() const {
  129.         return Iter(0, 0, body);
  130.     }
  131.     Iter begin() {
  132.         return Iter(0, 0, body);
  133.     }
  134.     Iter end() const {
  135.         return Iter(body.size(), 0, body);
  136.     }
  137.     Iter end() {
  138.         return Iter(body.size(), 0, body);
  139.     }
  140. };
  141. template <typename T>
  142. std::ostream& operator << (std::ostream& out, const Matrix<T>& mat) {
  143.     string line = "";
  144.     for (size_t i = 0; i != mat.size().first; ++i) {
  145.         string space = "";
  146.         out << line;
  147.         for (size_t j = 0; j != mat.size().second; ++j) {
  148.             out << space << mat(i, j);
  149.             space = "\t";
  150.         }
  151.         line = "\n";
  152.     }
  153.     return out;
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement