35657

Untitled

Jul 17th, 2023
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.28 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.             throw logic_error("Невалидный индекс");
  69.         }
  70.         return arr[index];
  71.     } // перегрузка оператора индексирования (сеттер)
  72.  
  73.     T operator[] (const int index) const {
  74.         if (index < 0 || index >= size) {
  75.             throw logic_error("Невалидный индекс");
  76.         }
  77.         return arr[index];
  78.     } // перегрузка оператора индексирования для константных объектов
  79.  
  80.     bool operator==(const Vector& right) const {
  81.         if (right.size != size) {
  82.             return false;
  83.         }
  84.         for (int i = 0; i < size; i++) {
  85.             if (arr[i] != right.arr[i])
  86.                 return false;
  87.         }
  88.         return true;
  89.     }
  90.  
  91.     bool operator!=(const Vector& right) const {
  92.         return !(*this == right);
  93.     }
  94.  
  95.     void Push_back(const T value) {
  96.         if (size == capacity) {
  97.             T* temp = new T[capacity * 2];
  98.             for (int i = 0; i < size; i++) {
  99.                 temp[i] = arr[i];
  100.             }
  101.             delete[] arr;
  102.             arr = temp;
  103.             capacity *= 2;
  104.         }
  105.         arr[size] = value;
  106.         size++;
  107.         total_number_elements++;
  108.     }
  109.  
  110.     void Pop_back() {
  111.         if (size > 0) {
  112.             arr[size - 1] = 0;
  113.             size--;
  114.             total_number_elements--;
  115.         }
  116.     }
  117.  
  118.     int Size() {
  119.         return size;
  120.     }
  121.  
  122.  
  123.     int Capacity() {
  124.         return capacity;
  125.     }
  126.  
  127.     void Insert(const int index, const T value) {
  128.         if (index < 0 || index >= size) {
  129.             throw logic_error("Невалидный индекс");
  130.         }
  131.         if (size == capacity) {
  132.             T* temp = new T[capacity * 2];
  133.             for (int i = 0; i < size; i++) {
  134.                 temp[i] = arr[i];
  135.             }
  136.             delete[] arr;
  137.             arr = temp;
  138.             capacity *= 2;
  139.         }
  140.         for (int i = size; i > index; i--) {
  141.             arr[i] = arr[i - 1];
  142.         }
  143.         arr[index] = value;
  144.         size++;
  145.         total_number_elements++;
  146.     }
  147.  
  148.     void Erase(const int index) {
  149.         if (index < 0 || index >= size) {
  150.             throw logic_error("Невалидный индекс");
  151.         }
  152.         for (int i = index; i < size - 1; i++) {
  153.             arr[i] = arr[i + 1];
  154.         }
  155.         arr[size - 1] = 0;
  156.         size--;
  157.         total_number_elements--;
  158.     }
  159.  
  160.     T GetValue(const int index) {
  161.         if (index < 0 || index >= size) {
  162.             throw logic_error("Невалидный индекс");
  163.         }
  164.         return arr[index];
  165.     }
  166.  
  167.     void SetValue(const int index, const T value) {
  168.         if (index < 0 || index >= size) {
  169.             throw logic_error("Невалидный индекс");
  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.     T* arr;
  188.     static int total_number_elements;
  189. };
  190.  
  191. int Vector<int>::total_number_elements = 0;
  192. int Vector<string>::total_number_elements = 0;
  193.  
  194. int main() {
  195.  
  196.     setlocale(LC_ALL, "ru");
  197.  
  198.     try {
  199.  
  200.         Vector<int> my_vector;
  201.  
  202.         my_vector.Push_back(10);
  203.  
  204.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  205.  
  206.         my_vector.Erase(0);
  207.  
  208.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  209.  
  210.         for (int i = 0; i < 10; i++) {
  211.             my_vector.Push_back(i + 1);
  212.         }
  213.  
  214.         for (int i = 0; i < my_vector.Size(); i++) {
  215.             cout << my_vector[i] << " ";
  216.         }
  217.         cout << endl;
  218.  
  219.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  220.  
  221.         my_vector[2] = 13;
  222.  
  223.         my_vector.Insert(4, 12);
  224.  
  225.         for (int i = 0; i < my_vector.Size(); i++) {
  226.             cout << my_vector[i] << " ";
  227.         }
  228.         cout << endl;
  229.  
  230.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  231.  
  232.         Vector<int> my_vector2;
  233.  
  234.         for (int i = 0; i < 15; i++) {
  235.             my_vector2.Push_back(i + 1);
  236.         }
  237.  
  238.  
  239.         Vector my_vector3(my_vector2);
  240.  
  241.         Vector my_vector4 = my_vector;
  242.  
  243.         cout << boolalpha;
  244.  
  245.         cout << (my_vector == my_vector3) << endl;
  246.  
  247.         cout << (my_vector != my_vector3) << endl;
  248.  
  249.         cout << my_vector3[3] << endl;
  250.  
  251.         Vector my_vector5(move(my_vector3));
  252.         cout << "Size: " << my_vector5.Size() << " Capacity: " << my_vector5.Capacity() << endl;
  253.         cout << "Size: " << my_vector3.Size() << " Capacity: " << my_vector3.Capacity() << endl;
  254.  
  255.         my_vector5 = move(my_vector4);
  256.         cout << "Size: " << my_vector5.Size() << " Capacity: " << my_vector5.Capacity() << endl;
  257.         cout << "Size: " << my_vector3.Size() << " Capacity: " << my_vector3.Capacity() << endl;
  258.         cout << Vector<int>::GetTotalNumberElements() << endl;
  259.  
  260.         Vector<string> my_vector6;
  261.         for (int i = 0; i < 8; i++) {
  262.             my_vector6.Push_back("123");
  263.         }
  264.         Vector my_vector7(my_vector6);
  265.         Vector<int> my_vector8{ 1, 2, 3, 4 };
  266.         cout << my_vector6[0] << " Size: " << my_vector6.Size() << " Capacity: " << my_vector6.Capacity() << endl;
  267.         cout << my_vector7[0] << " Size: " << my_vector7.Size() << " Capacity: " << my_vector7.Capacity() << endl;
  268.         cout << my_vector8[0] << " Size: " << my_vector8.Size() << " Capacity: " << my_vector8.Capacity() << endl;
  269.         cout << Vector<int>::GetTotalNumberElements() << endl;
  270.         cout << Vector<string>::GetTotalNumberElements() << endl;
  271.     }
  272.     catch (const logic_error& ex) { // для проверки logic_error можно удалить элемент по несуществующему индексу
  273.         cout << ex.what() << endl;
  274.     }
  275.     catch (const bad_alloc& ex) { // для проверки bad_alloc можно добавить в вектор 100000000000 элементов
  276.         cout << "Ошибка выделения динамической памяти" << endl;
  277.     }
  278.     catch (...) { // для проверки можно поменять тип ошибки в Erase на runtime_error и вызвать её через некорректный индекс
  279.         cout << "Другая ошибка" << endl;
  280.     }
  281. }
Advertisement
Add Comment
Please, Sign In to add comment