SHARE
TWEET

Untitled

a guest Feb 20th, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <typename T>
  5. class Matrix {
  6. private:
  7.     std::vector<std::vector<T>> mass;
  8. public:
  9.     Matrix(const std::vector<std::vector<T>>& m) {
  10.         mass = m;
  11.     }
  12.  
  13.     const std::pair<size_t, size_t> size() const {
  14.         std::pair<size_t, size_t> res;
  15.         res.first = mass.size();
  16.         res.second = mass[0].size();
  17.         return res;
  18.     }
  19.  
  20.     std::vector<T>& operator[] (size_t i) {
  21.         return mass[i];
  22.     }
  23.  
  24.     const std::vector<T> operator[] (size_t i) const {
  25.         return mass[i];
  26.     }
  27. };
  28.  
  29. template <typename T>
  30. std::ostream& operator << (std::ostream& out, const Matrix<T>& m) {
  31.     for (size_t i = 0; i < m.size().first; ++i) {
  32.         for (size_t j = 0; j < m.size().second; ++j) {
  33.             if (j == m.size().second - 1) {
  34.                 out << m[i][j];
  35.             } else {
  36.                 out << m[i][j] << "\t";
  37.             }
  38.         }
  39.         if (i != m.size().first - 1) {
  40.             out << "\n";
  41.         }
  42.     }
  43.     return out;
  44. }
  45.  
  46. template <typename T>
  47. Matrix<T>& operator += (Matrix<T>& first, const Matrix<T>& second) {
  48.     for (size_t i = 0; i < first.size().first; ++i) {
  49.         for (size_t j = 0; j < second.size().second; ++j) {
  50.             first[i][j] += second[i][j];
  51.         }
  52.     }
  53.     return first;
  54. }
  55.  
  56. template <typename T>
  57. Matrix<T> operator + (const Matrix<T>& first, const Matrix<T>& second) {
  58.     Matrix<T> a = first;
  59.     a += second;
  60.     return a;
  61. }
  62.  
  63. template <typename T, typename N>
  64. Matrix<T>& operator *= (Matrix<T>& first, const N& x) {
  65.     for (size_t i = 0; i < first.size().first; ++i) {
  66.         for (size_t j = 0; j < first.size().second; ++j) {
  67.             first[i][j] *= x;
  68.         }
  69.     }
  70.     return first;
  71. }
  72.  
  73. template <typename T, typename N>
  74. Matrix<T> operator * (const Matrix<T>& first, const N& x) {
  75.     Matrix<T> a = first;
  76.     a *= x;
  77.     return a;
  78. }
  79.  
  80. int main() {
  81.     int m, n;
  82.     std::cin >> n >> m;
  83.     std::vector<std::vector<int>> a(n, std::vector<int>(m));
  84.     for (size_t i = 0; i < a.size(); ++i) {
  85.         for (size_t j = 0; j < a[0].size(); ++j) {
  86.             std::cin >> a[i][j];
  87.         }
  88.     }
  89.     Matrix<int> b = a;
  90.     Matrix<int> c = a;
  91.     b += c *= 3;
  92.     std::cout << b;
  93. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top