Advertisement
35657

Untitled

May 26th, 2023
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1.  
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. class Vector {
  7.  
  8. public:
  9.  
  10. Vector() : size(0), capacity(4), arr(new int[4]) {}
  11.  
  12. Vector(const int vector_capacity) : size(0), capacity(vector_capacity), arr(new int[vector_capacity]) {}
  13.  
  14. Vector(const Vector& other) : size(other.size), capacity(other.capacity), arr(new int[capacity]) {
  15. for (int i = 0; i < size; i++) {
  16. arr[i] = other.arr[i];
  17. }
  18. total_number_elements += size;
  19. }
  20.  
  21. void Push_back(const int value) {
  22. if (size == capacity) {
  23. int* temp = new int[capacity * 2];
  24. for (int i = 0; i < size; i++) {
  25. temp[i] = arr[i];
  26. }
  27. delete[] arr;
  28. arr = temp;
  29. capacity *= 2;
  30. }
  31. arr[size] = value;
  32. size++;
  33. total_number_elements++;
  34. }
  35.  
  36. void Pop_back() {
  37. if (size > 0) {
  38. arr[size - 1] = 0;
  39. size--;
  40. total_number_elements--;
  41. }
  42. }
  43.  
  44. int Size() {
  45. return size;
  46. }
  47.  
  48.  
  49. int Capacity() {
  50. return capacity;
  51. }
  52.  
  53. void Insert(const int index, const int value) {
  54. if (index < 0 || index >= size) {
  55. cout << "Invalid index" << endl;
  56. return;
  57. }
  58. if (size == capacity) {
  59. int* temp = new int[capacity * 2];
  60. for (int i = 0; i < size; i++) {
  61. temp[i] = arr[i];
  62. }
  63. delete[] arr;
  64. arr = temp;
  65. capacity *= 2;
  66. }
  67. for (int i = size; i > index; i--) {
  68. arr[i] = arr[i - 1];
  69. }
  70. arr[index] = value;
  71. size++;
  72. total_number_elements++;
  73. }
  74.  
  75. void Erase(const int index) {
  76. if (index < 0 || index >= size) {
  77. cout << "Invalid index" << endl;
  78. return;
  79. }
  80. for (int i = index; i < size - 1; i++) {
  81. arr[i] = arr[i + 1];
  82. }
  83. arr[size - 1] = 0;
  84. size--;
  85. total_number_elements--;
  86. }
  87.  
  88. int GetValue(const int index) {
  89. if (index < 0 || index >= size) {
  90. cout << "Invalid index" << endl;
  91. abort;
  92. }
  93. return arr[index];
  94. }
  95.  
  96. void SetValue(const int index, const int value) {
  97. if (index < 0 || index >= size) {
  98. cout << "Invalid index" << endl;
  99. return;
  100. }
  101. arr[index] = value;
  102. }
  103.  
  104. static int GetTotalNumberElements() {
  105. return total_number_elements;
  106. }
  107.  
  108.  
  109. ~Vector() {
  110. delete[] arr;
  111. total_number_elements -= size;
  112. }
  113.  
  114. private:
  115. int size;
  116. int capacity;
  117. int* arr;
  118. static int total_number_elements;
  119. };
  120.  
  121. int Vector::total_number_elements = 0;
  122.  
  123. int main() {
  124.  
  125. Vector my_vector;
  126.  
  127. my_vector.Push_back(10);
  128.  
  129. cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  130.  
  131. my_vector.Erase(0);
  132.  
  133. cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  134.  
  135. for (int i = 0; i < 10; i++) {
  136. my_vector.Push_back(i + 1);
  137. }
  138.  
  139. for (int i = 0; i < my_vector.Size(); i++) {
  140. cout << my_vector.GetValue(i) << " ";
  141. }
  142. cout << endl;
  143.  
  144. cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  145.  
  146. my_vector.SetValue(2, 13);
  147.  
  148. my_vector.Insert(4, 12);
  149.  
  150. for (int i = 0; i < my_vector.Size(); i++) {
  151. cout << my_vector.GetValue(i) << " ";
  152. }
  153. cout << endl;
  154.  
  155. cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  156.  
  157. Vector my_vector2;
  158.  
  159. for (int i = 0; i < 15; i++) {
  160. my_vector2.Push_back(i + 1);
  161. }
  162.  
  163.  
  164. Vector my_vector3(my_vector2);
  165.  
  166.  
  167. cout << Vector::GetTotalNumberElements() << endl;
  168.  
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement