fork download
  1. #include <iostream>
  2.  
  3. template <typename T>
  4. class MyVector
  5. {
  6. private:
  7. unsigned vtr_size;
  8. unsigned capacity;
  9. T* elements;
  10.  
  11. public:
  12. MyVector(unsigned size, T default_value = T())
  13. : vtr_size(size), capacity(size), elements(new T[size])
  14. {
  15. for (unsigned i = 0; i < vtr_size; ++i)
  16. elements[i] = default_value;
  17. }
  18.  
  19. T at(unsigned index) const
  20. {
  21. if ( index < vtr_size)
  22. return elements[index];
  23.  
  24. return T();
  25. }
  26.  
  27. unsigned size() const { return vtr_size; }
  28. };
  29.  
  30. template <typename container>
  31. void print(const container& c)
  32. {
  33. for (unsigned i = 0; i < c.size(); ++i)
  34. std::cout << c.at(i) << ' ';
  35. std::cout << '\n';
  36. }
  37.  
  38. int main()
  39. {
  40. MyVector<int> V1(5);
  41. MyVector<int> V2(5, 4);
  42.  
  43. print(V1);
  44. std::cout << '\n';
  45.  
  46. print(V2);
  47. std::cout << '\n';
  48. }
  49.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
0 0 0 0 0 

4 4 4 4 4