35657

Untitled

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