35657

Untitled

Sep 7th, 2024
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.12 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.             throw runtime_error("Пустой контейнер");
  113.         }
  114.         arr[size - 1] = 0;
  115.         size--;
  116.         total_number_elements--;
  117.     }
  118.  
  119.     int Size() {
  120.         return size;
  121.     }
  122.  
  123.  
  124.     int Capacity() {
  125.         return capacity;
  126.     }
  127.  
  128.     void Insert(const int index, const T value) {
  129.         if (index < 0 || index >= size) {
  130.             throw logic_error("Невалидный индекс");
  131.         }
  132.         if (size == capacity) {
  133.             T* temp = new T[capacity * 2];
  134.             for (int i = 0; i < size; i++) {
  135.                 temp[i] = arr[i];
  136.             }
  137.             delete[] arr;
  138.             arr = temp;
  139.             capacity *= 2;
  140.         }
  141.         for (int i = size; i > index; i--) {
  142.             arr[i] = arr[i - 1];
  143.         }
  144.         arr[index] = value;
  145.         size++;
  146.         total_number_elements++;
  147.     }
  148.  
  149.     void Erase(const int index) {
  150.         if (index < 0 || index >= size) {
  151.             throw logic_error("Невалидный индекс");
  152.         }
  153.         for (int i = index; i < size - 1; i++) {
  154.             arr[i] = arr[i + 1];
  155.         }
  156.         arr[size - 1] = 0;
  157.         size--;
  158.         total_number_elements--;
  159.     }
  160.  
  161.     T GetValue(const int index) {
  162.         if (index < 0 || index >= size) {
  163.             throw logic_error("Невалидный индекс");
  164.         }
  165.         return arr[index];
  166.     }
  167.  
  168.     void SetValue(const int index, const T value) {
  169.         if (index < 0 || index >= size) {
  170.             throw logic_error("Невалидный индекс");
  171.         }
  172.         arr[index] = value;
  173.     }
  174.  
  175.     static int GetTotalNumberElements() {
  176.         return total_number_elements;
  177.     }
  178.  
  179.     void Print() {
  180.         for (int i = 0; i < size; i++) {
  181.             cout << arr[i] << " ";
  182.         }
  183.         cout << endl;
  184.     }
  185.  
  186.     ~Vector() {
  187.         delete[] arr;
  188.         total_number_elements -= size;
  189.     }
  190.  
  191. private:
  192.     int size;
  193.     int capacity;
  194.     T* arr;
  195.     static int total_number_elements;
  196. };
  197.  
  198. int Vector<int>::total_number_elements = 0;
  199. int Vector<string>::total_number_elements = 0;
  200.  
  201. int main() {
  202.  
  203.     setlocale(LC_ALL, "ru");
  204.  
  205.     try {
  206.         Vector<int> my_vector;
  207.  
  208.         my_vector.Push_back(10);
  209.  
  210.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  211.  
  212.         my_vector.Erase(0);
  213.  
  214.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  215.  
  216.         for (int i = 0; i < 10; i++) {
  217.             my_vector.Push_back(i + 1);
  218.         }
  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.         my_vector[2] = 13;
  228.  
  229.         my_vector.Insert(4, 12);
  230.  
  231.         for (int i = 0; i < my_vector.Size(); i++) {
  232.             cout << my_vector[i] << " ";
  233.         }
  234.         cout << endl;
  235.  
  236.         cout << "Size: " << my_vector.Size() << " Capacity: " << my_vector.Capacity() << endl;
  237.  
  238.         Vector<int> my_vector2;
  239.  
  240.         //my_vector2.Pop_back();
  241.  
  242.         for (int i = 0; i < 15; i++) {
  243.             my_vector2.Push_back(i + 1);
  244.         }
  245.  
  246.  
  247.         Vector<int> my_vector3(my_vector2);
  248.  
  249.         Vector<int> 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<int> 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<string> 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.     }
  280.     catch (const logic_error& er) {
  281.         cout << "_1_ " << er.what() << endl;
  282.     }
  283.     catch (const runtime_error& er) {
  284.         cout << "_2_ " << er.what() << endl;
  285.     }
  286.     catch (const bad_alloc& er) {
  287.         cout << "_3_ " << er.what() << endl;
  288.     }
  289.     catch (...) {
  290.         cout << "_4_ " << "Другая ошибка" << endl;
  291.     }
  292. }
Advertisement
Add Comment
Please, Sign In to add comment