35657

Untitled

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