Advertisement
35657

Untitled

May 17th, 2024
381
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.14 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T>
  6. class Vector {
  7.  
  8. public:
  9.     Vector() : size_(0), capacity_(4), arr_(new T[4]) {}
  10.  
  11.     explicit Vector(const int vector_capacity) : size_(0), capacity_(vector_capacity), arr_(new T[vector_capacity]) {}
  12.  
  13.     Vector(const Vector& other) : size_(other.size_), capacity_(other.capacity_), arr_(new T[capacity_]) {
  14.         for (int i = 0; i < size_; i++) {
  15.             arr_[i] = other.arr_[i];
  16.         }
  17.         total_number_elements_ += size_;
  18.     } // конструктор копирования
  19.  
  20.     Vector(const initializer_list<T>& list) : size_(list.size()), capacity_(list.size()), arr_(new T[list.size()]) {
  21.         int i = 0;
  22.         for (const T& element : list) {
  23.             arr_[i] = element;
  24.             i++;
  25.         }
  26.     }
  27.  
  28.     Vector& operator=(const Vector& other) {
  29.         if (&other != this) { // проверка на самоприсваивание
  30.             total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
  31.             size_ = other.size_;
  32.             capacity_ = other.capacity_;
  33.             delete[] arr_;
  34.             arr_ = new T[capacity_];
  35.             for (int i = 0; i < size_; i++) {
  36.                 arr_[i] = other.arr_[i];
  37.             }
  38.         }
  39.         return *this;
  40.     } // копирующий оператор присваивания =
  41.  
  42.     Vector(Vector&& other) : size_(other.size_), capacity_(other.capacity_), arr_(other.arr_) {
  43.         other.arr_ = nullptr;
  44.         other.size_ = 0;
  45.         other.capacity_ = 0;
  46.     } // конструктор перемещения
  47.  
  48.     Vector& operator=(Vector&& other) {
  49.         if (&other != this) {
  50.             total_number_elements_ += other.size_ - size_; // общее количество увеличивается (уменьшается) на разность элементов
  51.             size_ = other.size_;
  52.             capacity_ = other.capacity_;
  53.             delete[] arr_;
  54.             arr_ = other.arr_;
  55.             other.arr_ = nullptr;
  56.             other.size_ = 0;
  57.             other.capacity_ = 0;
  58.         }
  59.         return *this;
  60.     } // перемещающий оператор присваивания =
  61.  
  62.     void push_back(const T& value) {
  63.         check_capacity();
  64.         arr_[size_] = value;
  65.         size_++;
  66.         total_number_elements_++;
  67.     }
  68.  
  69.     void pop_back() {
  70.         if (size_ == 0) {
  71.             throw runtime_error("Попытка удаления в пустом контейнере");
  72.         }
  73.         size_--;
  74.         total_number_elements_--;
  75.     }
  76.  
  77.     void insert(const int index, const T& value) { // типы ошибок можно разделить
  78.         if (index < 0 || index >= size_) {
  79.             throw logic_error("Некорректный индекс");
  80.         }
  81.         check_capacity();
  82.         for (int i = size_; i > index; i--) {
  83.             arr_[i] = arr_[i - 1];
  84.         }
  85.         arr_[index] = value;
  86.         size_++;
  87.         total_number_elements_++;
  88.     }
  89.  
  90.     void erase(const int index) {
  91.         if (index < 0 || index >= size_) {
  92.             throw logic_error("Некорректный индекс");
  93.         }
  94.         for (int i = index; i < size_ - 1; i++) {
  95.             arr_[i] = arr_[i + 1];
  96.         }
  97.         size_--;
  98.         total_number_elements_--;
  99.     }
  100.  
  101.     T& operator[](const int index) {
  102.         if (index < 0 || index >= size_) {
  103.             throw logic_error("Некорректный индекс");
  104.         }
  105.         return arr_[index];
  106.     }
  107.  
  108.     const T& operator[](const int index) const {
  109.         if (index < 0 || index >= size_) {
  110.             throw logic_error("Некорректный индекс");
  111.         }
  112.         return arr_[index];
  113.     }
  114.  
  115.     int size() const {
  116.         return size_;
  117.     }
  118.  
  119.     int capacity() const {
  120.         return capacity_;
  121.     }
  122.  
  123.     bool operator==(const Vector& right) const {
  124.         if (right.size_ != size_) {
  125.             return false;
  126.         }
  127.         for (int i = 0; i < size_; i++) {
  128.             if (arr_[i] != right.arr_[i])
  129.                 return false;
  130.         }
  131.         return true;
  132.     }
  133.  
  134.     bool operator!=(const Vector& right) const {
  135.         return !(*this == right);
  136.     }
  137.  
  138.     void print() const {
  139.         for (int i = 0; i < size_; i++) {
  140.             cout << arr_[i] << " ";
  141.         }
  142.         cout << endl;
  143.     }
  144.  
  145.     static int get_total_number_elements() {
  146.         return total_number_elements_;
  147.     }
  148.  
  149.  
  150.     ~Vector() {
  151.         delete[] arr_;
  152.         total_number_elements_ -= size_;
  153.     }
  154.  
  155. private:
  156.  
  157.     void check_capacity() {
  158.         if (size_ == capacity_) {
  159.             int* temp = new int[capacity_ * 2];
  160.             for (int i = 0; i < size_; i++) {
  161.                 temp[i] = arr_[i];
  162.             }
  163.             delete[] arr_;
  164.             arr_ = temp;
  165.             capacity_ *= 2;
  166.         }
  167.     }
  168.  
  169.     int size_; // текущее количество элементов
  170.     int capacity_; // емкость хранилища
  171.     T* arr_; // хранилище
  172.     static int total_number_elements_; // общее количество элементов по всем векторам
  173. };
  174.  
  175. template <typename T>
  176. int Vector<T>::total_number_elements_ = 0;
  177.  
  178.  
  179. int main() {
  180.     setlocale(LC_ALL, "ru");
  181.  
  182.     try {
  183.  
  184.         Vector<int> vec{ 1,2,3,4,5 };
  185.  
  186.         vec.print();
  187.  
  188.        // vec.insert(9, 12);
  189.  
  190.         vec.print();
  191.  
  192.  
  193.         //for (int i = 0; i < 7; i++) {
  194.         //    vec.pop_back();
  195.         //}
  196.  
  197.  
  198.         //for (int i = 0; i < 10000000000; i++) {
  199.         //    vec.push_back(i);
  200.         //}
  201.     }
  202.     catch (const runtime_error& ex) {
  203.         cout << ex.what() << " 1 " << endl;
  204.     }
  205.     catch(const logic_error& ex) {
  206.         cout << ex.what() << " 2 " << endl;
  207.     }
  208.     catch (const bad_alloc& ex) {
  209.         cout << ex.what() << " 3 " << endl;
  210.     }
  211.     catch (...) { // ловим ошибки любого другого типа
  212.         cout << "Другая ошибка" << " 4 " << endl;
  213.     }
  214.    
  215.  
  216.    
  217.  
  218. }
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement