fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <sstream>
  4. using namespace std;
  5.  
  6. template<typename T>
  7. class Array {
  8. public:
  9. Array(int rs, int cs)
  10. :rows( rs ), cols( cs )
  11. {
  12. const int Length = rows * cols;
  13.  
  14. v = new T[ Length ];
  15. memset( v, 0, Length * sizeof( T ) );
  16. }
  17.  
  18. ~Array()
  19. {
  20. delete[] v;
  21. }
  22.  
  23. T &cell(int row, int col)
  24. {
  25. return v[ ( row * cols ) + col ];
  26. }
  27.  
  28. void setRow(int r, T * data)
  29. {
  30. memcpy( v + ( cols * r ), data, cols * sizeof( T ) );
  31. }
  32.  
  33. string toString()
  34. {
  35. const int Length = rows * cols;
  36. ostringstream cnvt;
  37.  
  38. for(int i = 0; i < Length; ++i) {
  39. cnvt << v[ i ];
  40. if ( ( i + 1 ) % cols == 0 ) {
  41. cnvt << '\n';
  42. }
  43. }
  44.  
  45. return cnvt.str();
  46. }
  47.  
  48. private:
  49. T * v;
  50. int rows;
  51. int cols;
  52. };
  53.  
  54. int main() {
  55. int data[][4] = {{2,1,2,1},
  56. {1,2,2,2},
  57. {1,1,1,1},
  58. {2,1,1,2}};
  59. Array<int> array( 4, 4 );
  60.  
  61. array.setRow( 0, data[ 0 ] );
  62. array.setRow( 1, data[ 1 ] );
  63. array.setRow( 2, data[ 2 ] );
  64. array.setRow( 3, data[ 3 ] );
  65.  
  66. cout << array.toString() << endl;
  67. return 0;
  68. }
  69.  
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
2121
1222
1111
2112