Advertisement
35657

Untitled

May 21st, 2023
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  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.     ~Vector() {
  98.         delete[] arr;
  99.     }
  100.  
  101. private:
  102.     int size;
  103.     int capacity;
  104.     int* arr;
  105. };
  106.  
  107. int Vector::total_number_elements = 0;
  108.  
  109. int main() {
  110.  
  111.     Vector my_vector;
  112.  
  113.     my_vector.Push_back(10);
  114.  
  115.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  116.  
  117.     my_vector.Erase(0);
  118.  
  119.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  120.  
  121.     for (int i = 0; i < 10; i++) {
  122.         my_vector.Push_back(i + 1);
  123.     }
  124.  
  125.     for (int i = 0; i < my_vector.Size(); i++) {
  126.         cout << my_vector.GetValue(i) << " ";
  127.     }
  128.     cout << endl;
  129.  
  130.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  131.  
  132.     my_vector.SetValue(2, 13);
  133.  
  134.     my_vector.Insert(4, 12);
  135.  
  136.     for (int i = 0; i < my_vector.Size(); i++) {
  137.         cout << my_vector.GetValue(i) << " ";
  138.     }
  139.     cout << endl;
  140.  
  141.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement