fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <utility>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class Matrix {
  8. vector<vector<T>> m;
  9. public:
  10. Matrix (int r, int c) : m(r, vector<int>(c, T())) { }
  11. void show() {
  12. cout << "Matrix("<<m.size()<<","<<m[0].size()<<")"<<endl;
  13. for (int i=0; i<m.size(); i++) {
  14. cout <<" ";
  15. for (int j=0; j<m[i].size(); j++)
  16. cout << m[i][j]<<" ";
  17. cout<<endl;
  18. }
  19. }
  20. vector<T>& operator[] (int r) { return m[r]; }
  21. T& operator[] (pair<int, int> p) { return m[p.first][p.second]; }
  22. T& operator() (int r, int c) { return m[r][c]; }
  23. };
  24.  
  25.  
  26. int main() {
  27. Matrix<int> a(2,2);
  28. a.show();
  29.  
  30. a[{0,0}] = 3;
  31. int b = a[{1,0}];
  32. a.show();
  33.  
  34. a[0][1] = 4;
  35. int c = a[0][1];
  36. a.show();
  37.  
  38. a(1,0) = 5;
  39. int e = a(1,0);
  40. a.show();
  41.  
  42.  
  43. return 0;
  44. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
Matrix(2,2)
  0 0 
  0 0 
Matrix(2,2)
  3 0 
  0 0 
Matrix(2,2)
  3 4 
  0 0 
Matrix(2,2)
  3 4 
  5 0