35657

Untitled

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