fork download
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <cstdlib>
  5.  
  6. template<class T>
  7. class mat
  8. {
  9. unsigned int nrows, ncols;
  10. std::vector< T > data;
  11.  
  12. public:
  13. mat(unsigned int rows, unsigned int cols)
  14. : nrows(rows), ncols(cols)
  15. , data(rows * cols)
  16. {
  17. }
  18.  
  19. T* operator[](unsigned int row)
  20. {
  21. return &data.at(row * ncols);
  22. }
  23.  
  24. const T* operator[](unsigned int row) const
  25. {
  26. return &data.at(row * ncols);
  27. }
  28. };
  29.  
  30. int main()
  31. {
  32. mat<int> imat(50,50);
  33. mat<float> fmat(30,40);
  34.  
  35. imat[10][20] = 10;
  36. fmat[0][39] = 100.0;
  37.  
  38. return EXIT_SUCCESS;
  39. }
  40.  
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
Standard output is empty