Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1.  
  2. template <typename T>
  3. class Vector {
  4. public:
  5. Vector() {
  6. capacity = 100;
  7. size = 0;
  8. data = new T[capacity];
  9. }
  10. Vector(int capacity) {
  11. this->capacity = capacity;
  12. size = 0;
  13. data = new T[capacity];
  14. }
  15. ~Vector()
  16. {
  17. delete[] data;
  18. }
  19.  
  20. //добавляем в конец массива
  21. void Push(T value) {
  22. if (size + 1 > capacity) {
  23. capacity += 100;
  24. T* newMem = new T[capacity];
  25. memcpy(newMem, data, sizeof(T) * size);
  26. delete data;
  27. data = newMem;
  28. }
  29.  
  30. data[size] = value;
  31. size++;
  32. }
  33.  
  34. int Size() {
  35. return size;
  36. }
  37.  
  38. T& operator[](int index) {
  39. return data[index];
  40. }
  41.  
  42. void Remove(int index) {
  43. if ((size - 1) > 0) {
  44. memcpy(data + index, data + index + 1, (size - index - 1) * sizeof(T));
  45. // | здесь мы смещаем то что после
  46. //удаляемого элемента на его место;
  47. }
  48. size--;
  49. }
  50.  
  51. void Insert(int index, T key) {
  52. if (size + 1 > capacity) {
  53. capacity += 100;
  54. T* t = new T[capacity];
  55. memcpy(t, data, size * sizeof(T));
  56. delete data; //очистка памяти
  57. data = t;
  58. }
  59.  
  60. memcpy(data + index + 1, data + index, (size - index) * sizeof(T));
  61. data[index] = key;
  62. size++;
  63. }
  64.  
  65. class Iterator {
  66. public:
  67. Iterator(T* cur) {
  68. current = cur;
  69. }
  70.  
  71. bool operator!=(const Iterator& other) {
  72. return current != other.current;
  73. }
  74.  
  75. Iterator& operator++() {
  76. current = ++current;
  77. return *this;
  78. }
  79. Iterator& operator++(int) {
  80. Iterator i = *this;
  81. current = ++current;
  82. return i;
  83. }
  84.  
  85. T operator*() {
  86. return *current;
  87. }
  88. private:
  89. T* current;
  90. };
  91.  
  92. class ReverseIterator {
  93. private:
  94. T* current;
  95. public:
  96. ReverseIterator(T* cur) {
  97. current = cur;
  98. }
  99. T operator*() {
  100. return *current;
  101. }
  102.  
  103. bool operator!=(const ReverseIterator& other) {
  104. return current != other.current;
  105. }
  106.  
  107. ReverseIterator& operator++() { //префикс
  108. current = --current;
  109. return *this;
  110. }
  111.  
  112. ReverseIterator& operator++(int) { //постфикс
  113. ReverseIterator i = *this;
  114. current = --current;
  115. return i;
  116. }
  117. };
  118.  
  119. Iterator Begin() {
  120. return Iterator(data);
  121. }
  122. Iterator End() {
  123. return Iterator(data + size);
  124. }
  125. ReverseIterator rBegin() {
  126. return ReverseIterator(data + size - 1);
  127. }
  128. ReverseIterator rEnd() {
  129. return ReverseIterator(data-1);
  130. }
  131.  
  132. private:
  133. int size;
  134. int capacity;
  135. T* data;
  136. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement