Advertisement
inwerp

Untitled

Jul 27th, 2017
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. template <class T>
  2. class CrapVec {
  3. int size;
  4. int capacity;
  5. T *data;
  6. T *data_new;
  7.  
  8. public:
  9. ~CrapVec ( )
  10. {
  11. delete[] data;
  12. }
  13. CrapVec ( )
  14. {
  15.  
  16. size = 0;
  17. capacity = 1;
  18. T* data = new T[capacity];
  19. }
  20.  
  21. T& operator[] (int index) {
  22. return data[index];
  23. }
  24. void PushBack (T x)
  25. {
  26. if (size >= capacity) {
  27. reallocate(capacity*2);
  28. capacity = capacity*2;
  29. }
  30. data[size++] = x;
  31. }
  32. void reallocate(size_t s)
  33. {
  34. if (s < capacity)
  35. {
  36. std::cout << "what are you doing mazafuka" << std::endl;
  37. return;
  38. }
  39. T* data_new = new T[s];
  40. for(int it = 0; it != size ; ++it)
  41. {
  42. data_new[it] = data[it];
  43.  
  44. }
  45. delete[] data;
  46. data = data_new;
  47.  
  48. }
  49. void Print()
  50. {
  51. std::cout << "Vactor values: ";
  52. for(int it = 0; it != size ; ++it)
  53. {
  54. std::cout << data[it] << ", ";
  55.  
  56. }
  57. std::cout << "." << std::endl;
  58. }
  59.  
  60. };
  61.  
  62.  
  63.  
  64.  
  65.  
  66. int main()
  67. {
  68. CrapVec<int> myvec;
  69. myvec.PushBack(1);
  70. //myvec.PushBack(2);
  71. //myvec.PushBack(4);
  72. //myvec.Print();
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement