Advertisement
35657

Untitled

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