fork download
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. template <class T>
  6. class matrix {
  7. protected:
  8. unsigned long width;
  9. unsigned long height;
  10. vector<vector<T>> v;
  11. public:
  12. matrix(unsigned long val_height, unsigned long val_width) :
  13. height(val_height),
  14. width(val_width),
  15. v(vector<vector<T>>(height, vector<T>(width, 0))) // так
  16. {
  17. //или вместо инициализации
  18. //v.resize(height, vector<T>(width, 0));
  19. }
  20.  
  21. ~matrix() {
  22. // у vector свой деструктор, который автоматически освободит память
  23. }
  24.  
  25. template <class U>
  26. friend ostream &operator << (ostream &s, const matrix<U> &m);
  27. };
  28.  
  29. template <class T>
  30. ostream &operator << (ostream &s, const matrix<T> &m) {
  31. for (int i = 0; i < m.height; i++) {
  32. for (int j = 0; j < m.width; j++) {
  33. s << m.v[i][j];
  34. }
  35. s << endl;
  36. }
  37. return s;
  38. }
  39.  
  40. int main() {
  41. matrix<int> a(2, 2);
  42. cout << a;
  43. return 0;
  44. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
00
00