fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <cassert>
  4. using namespace std;
  5. template<typename T,int Roz>
  6. class Matrix
  7. {
  8. public:
  9. T tab[Roz][Roz];
  10. int z;
  11. Matrix()
  12. {
  13. z=Roz;
  14. for(int i=0;i<Roz;++i)
  15. for(int j=0;j<Roz;++j)
  16. tab[i][j]=0;
  17. }
  18. T& operator()(int x,int y){
  19. return tab[x-1][y-1];
  20. }
  21.  
  22. template <typename T2, int W>
  23. Matrix operator+(const Matrix<T2,W>& b)
  24. {
  25. assert(b.z == Roz && this->z == Roz);
  26. Matrix<T2,Roz> tmp;
  27. for(int i=0;i<Roz;++i)
  28. for(int j=0;j<Roz;++j)
  29. tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
  30. return tmp;
  31. }
  32.  
  33. template <typename T2, int W>
  34. Matrix& operator=(const Matrix<T2,W>& b)
  35. {
  36. if(&b != this) {
  37. assert(Roz == W);
  38.  
  39. for(int i=0;i<Roz;++i)
  40. for(int j=0;j<Roz;++j)
  41. this->tab[i][j] = b.tab[i][j];
  42. }
  43. return *this;
  44. }
  45.  
  46. Matrix& operator*(Matrix<T,Roz> b)
  47. {
  48. Matrix<T,Roz> tmp;
  49. for(int i=0;i<Roz;++i)
  50. for(int j=0;j<Roz;++j)
  51. tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
  52. }
  53.  
  54. friend ostream& operator<<(ostream& out, Matrix<T,Roz> &v)
  55. {
  56. for(int i=0;i<v.z;++i)
  57. {
  58. for(int j=0;j<v.z;++j)
  59. out<<v.tab[i][j]<<" ";
  60. out<<endl;
  61. }
  62. return out;
  63. }
  64. };
  65.  
  66.  
  67. template<class T>
  68. class Matrix<T,0>
  69. {
  70. private:
  71. Matrix()
  72. {
  73. z=0;
  74. }
  75. public:
  76. vector<vector<T> > tab;
  77. int z;
  78.  
  79. Matrix(int z)
  80. {
  81. z = z;
  82. for(int i=0;i<z;++i)
  83. tab.push_back(vector<T>(z));
  84. this->z = z;
  85. for(int i=0;i<z;++i)
  86. for(int j=0;j<z;++j)
  87. tab[i][j]=0;
  88. }
  89. T& operator()(int x,int y){
  90. return tab[x-1][y-1];
  91. }
  92.  
  93. Matrix& operator+(Matrix<T,0> b)
  94. {
  95. Matrix<T,0> tmp(b.z);
  96. for(int i=0;i<z;++i)
  97. for(int j=0;j<z;++j)
  98. tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
  99.  
  100.  
  101. }
  102.  
  103. Matrix& operator+(Matrix<T,0> &b)
  104. {
  105. Matrix<T,0> tmp(b.z);
  106. for(int i=0;i<z;++i)
  107. for(int j=0;j<z;++j)
  108. tmp.tab[i][j]=this->tab[i][j]+b.tab[i][j];
  109.  
  110. }
  111.  
  112.  
  113.  
  114. friend ostream& operator<<(ostream& out, Matrix<T,0> &v)
  115. {
  116.  
  117. for(int i=0;i<v.z;++i)
  118. {
  119. for(int j=0;j<v.z;++j)
  120. out<<v.tab[i][j]<<" ";
  121.  
  122. out<<endl;
  123. }
  124. return out;
  125. }
  126. };
  127.  
  128.  
  129. int main()
  130. {
  131. Matrix<int,3> A;
  132. Matrix<int, 0> Z(3);
  133. Matrix<int, 3> Y;
  134. A(1,1)=1;
  135. Z(1,1)=1;
  136. std::cout << A << std::endl;
  137. std::cout << Z << std::endl;
  138. std::cout << Y << std::endl;
  139.  
  140. Y = A + Z;
  141. std::cout << Y << std::endl;
  142. return 0;
  143. }
Success #stdin #stdout 0s 2988KB
stdin
Standard input is empty
stdout
1 0 0 
0 0 0 
0 0 0 

1 0 0 
0 0 0 
0 0 0 

0 0 0 
0 0 0 
0 0 0 

2 0 0 
0 0 0 
0 0 0