fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. template <class T>
  5. class Vector {
  6. public:
  7. unsigned int rows;
  8. unsigned int cols;
  9. T** elements;
  10. Vector(unsigned int, unsigned int);
  11. ~Vector();
  12. };
  13.  
  14. template <class T>
  15. Vector<T>::Vector(unsigned int rows, unsigned int cols) {
  16. this->rows = rows;
  17. this->cols = cols;
  18.  
  19. this->elements = new T*[this->rows];
  20. for (int i = 0; i < this->rows; i++) {
  21. this->elements[i] = new T[this->cols];
  22. }
  23. }
  24.  
  25. template <class T>
  26. Vector<T>::~Vector() {
  27. for(int i=0; i<this->rows;++i)
  28. delete[] this->elements[i];
  29. delete[] this->elements;
  30. };
  31.  
  32.  
  33. int main()
  34. {
  35. Vector<int> obj(2,2);
  36. obj.elements[0][0] = 1;
  37. obj.elements[0][1] = 2;
  38. obj.elements[1][0] = 3;
  39. obj.elements[1][1] = 4;
  40.  
  41. std::cout << obj.elements[0][0] << " " << obj.elements[0][1] << std::endl;
  42. std::cout << obj.elements[1][0] << " " << obj.elements[1][1] << std::endl;
  43.  
  44. return 0;
  45. }
  46.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
1 2
3 4