35657

Untitled

Jul 19th, 2023
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.58 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.     Vector& operator=(const Vector& other) {
  22.         if (&other != this) {
  23.             total_number_elements -= size;
  24.             size = other.size;
  25.             capacity = other.capacity;
  26.             delete[] arr;
  27.             arr = new int[capacity];
  28.             for (int i = 0; i < size; i++) {
  29.                 arr[i] = other.arr[i];
  30.             }
  31.             total_number_elements += size;
  32.         }
  33.         return *this;
  34.     } // копирующий оператор присваивания =
  35.  
  36.     Vector(Vector&& other) : size(other.size), capacity(other.capacity), arr(other.arr) {
  37.         other.arr = nullptr;
  38.         other.size = 0;
  39.         other.capacity = 0;
  40.     } // конструктор перемещения
  41.  
  42.     Vector& operator=(Vector&& other) {
  43.         if (&other != this) {
  44.             total_number_elements -= size;
  45.             size = other.size;
  46.             capacity = other.capacity;
  47.             delete[] arr;
  48.             arr = other.arr;
  49.             other.arr = nullptr;
  50.             other.size = 0;
  51.             other.capacity = 0;
  52.             total_number_elements += size;
  53.         }
  54.         return *this;
  55.     } // перемещающий оператор присваивания =
  56.  
  57.     int& operator[] (const int index) {
  58.         if (index < 0 || index >= size) {
  59.             cout << "Invalid index" << endl;
  60.             abort;
  61.         }
  62.         return arr[index];
  63.     } // перегрузка оператора индексирования (сеттер)
  64.  
  65.     int operator[] (const int index) const {
  66.         if (index < 0 || index >= size) {
  67.             cout << "Invalid index" << endl;
  68.             abort;
  69.         }
  70.         return arr[index];
  71.     } // перегрузка оператора индексирования для константных объектов
  72.  
  73.     bool operator==(const Vector& right) const {
  74.         if (right.size != size) {
  75.             return false;
  76.         }
  77.         for (int i = 0; i < size; i++) {
  78.             if (arr[i] != right.arr[i])
  79.                 return false;
  80.         }
  81.         return true;
  82.     }
  83.  
  84.     bool operator!=(const Vector& right) const {
  85.         return !(*this == right);
  86.     }
  87.  
  88.     void Push_back(const int value) {
  89.         if (size == capacity) {
  90.             int* temp = new int[capacity * 2];
  91.             for (int i = 0; i < size; i++) {
  92.                 temp[i] = arr[i];
  93.             }
  94.             delete[] arr;
  95.             arr = temp;
  96.             capacity *= 2;
  97.         }
  98.         arr[size] = value;
  99.         size++;
  100.         total_number_elements++;
  101.     }
  102.  
  103.     void Pop_back() {
  104.         if (size > 0) {
  105.             arr[size - 1] = 0;
  106.             size--;
  107.             total_number_elements--;
  108.         }
  109.     }
  110.  
  111.     int Size() {
  112.         return size;
  113.     }
  114.  
  115.  
  116.     int Capacity() {
  117.         return capacity;
  118.     }
  119.  
  120.     void Insert(const int index, const int value) {
  121.         if (index < 0 || index >= size) {
  122.             cout << "Invalid index" << endl;
  123.             return;
  124.         }
  125.         if (size == capacity) {
  126.             int* temp = new int[capacity * 2];
  127.             for (int i = 0; i < size; i++) {
  128.                 temp[i] = arr[i];
  129.             }
  130.             delete[] arr;
  131.             arr = temp;
  132.             capacity *= 2;
  133.         }
  134.         for (int i = size; i > index; i--) {
  135.             arr[i] = arr[i - 1];
  136.         }
  137.         arr[index] = value;
  138.         size++;
  139.         total_number_elements++;
  140.     }
  141.  
  142.     void Erase(const int index) {
  143.         if (index < 0 || index >= size) {
  144.             cout << "Invalid index" << endl;
  145.             return;
  146.         }
  147.         for (int i = index; i < size - 1; i++) {
  148.             arr[i] = arr[i + 1];
  149.         }
  150.         arr[size - 1] = 0;
  151.         size--;
  152.         total_number_elements--;
  153.     }
  154.  
  155.     int GetValue(const int index) {
  156.         if (index < 0 || index >= size) {
  157.             cout << "Invalid index" << endl;
  158.             abort;
  159.         }
  160.         return arr[index];
  161.     }
  162.  
  163.     void SetValue(const int index, const int value) {
  164.         if (index < 0 || index >= size) {
  165.             cout << "Invalid index" << endl;
  166.             return;
  167.         }
  168.         arr[index] = value;
  169.     }
  170.  
  171.     static int GetTotalNumberElements() {
  172.         return total_number_elements;
  173.     }
  174.  
  175.  
  176.     ~Vector() {
  177.         delete[] arr;
  178.         total_number_elements -= size;
  179.     }
  180.  
  181. private:
  182.     int size;
  183.     int capacity;
  184.     int* arr;
  185.     static int total_number_elements;
  186. };
  187.  
  188. int Vector::total_number_elements = 0;
  189.  
  190. int main() {
  191.  
  192.     Vector my_vector;
  193.  
  194.     my_vector.Push_back(10);
  195.  
  196.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  197.  
  198.     my_vector.Erase(0);
  199.  
  200.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  201.  
  202.     for (int i = 0; i < 10; i++) {
  203.         my_vector.Push_back(i + 1);
  204.     }
  205.  
  206.     for (int i = 0; i < my_vector.Size(); i++) {
  207.         cout << my_vector[i] << " ";
  208.     }
  209.     cout << endl;
  210.  
  211.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  212.  
  213.     my_vector[2] = 13;
  214.  
  215.     my_vector.Insert(4, 12);
  216.  
  217.     for (int i = 0; i < my_vector.Size(); i++) {
  218.         cout << my_vector[i] << " ";
  219.     }
  220.     cout << endl;
  221.  
  222.     cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  223.  
  224.     Vector my_vector2;
  225.  
  226.     for (int i = 0; i < 15; i++) {
  227.         my_vector2.Push_back(i + 1);
  228.     }
  229.  
  230.  
  231.     Vector my_vector3(my_vector2);
  232.  
  233.     Vector my_vector4 = my_vector;
  234.  
  235.     cout << boolalpha;
  236.  
  237.     cout << (my_vector == my_vector3) << endl;
  238.  
  239.     cout << (my_vector != my_vector3) << endl;
  240.  
  241.     cout << my_vector3[3] << endl;
  242.  
  243.  
  244.     cout << Vector::GetTotalNumberElements() << endl;
  245.  
  246.     Vector my_vector5(move(my_vector3));
  247.     cout << "Size: " << my_vector5.Size() << " Capacity: " << my_vector5.Capacity() << endl;
  248.     cout << "Size: " << my_vector3.Size() << " Capacity: " << my_vector3.Capacity() << endl;
  249.  
  250.     my_vector5 = move(my_vector4);
  251.     cout << "Size: " << my_vector5.Size() << " Capacity: " << my_vector5.Capacity() << endl;
  252.     cout << "Size: " << my_vector3.Size() << " Capacity: " << my_vector3.Capacity() << endl;
  253.  
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment