35657

Untitled

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