35657

Untitled

Nov 11th, 2023
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.38 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.     void push_back(const int value) {
  22.         check_capacity_();
  23.         arr_[size_] = value;
  24.         size_++;
  25.         total_number_elements_++;
  26.     }
  27.  
  28.     void insert(const int index, const int value) {
  29.         if (index < 0 || index >= size_) {
  30.             cout << "Invalid index" << endl;
  31.             return;
  32.         }
  33.         check_capacity_();
  34.         for (int i = size_; i > index; i--) {
  35.             arr_[i] = arr_[i - 1];
  36.         }
  37.         arr_[index] = value;
  38.         size_++;
  39.         total_number_elements_++;
  40.     }
  41.  
  42.     void erase(const int index) {
  43.         if (index < 0 || index >= size_) {
  44.             cout << "Invalid index" << endl;
  45.             return;
  46.         }
  47.         for (int i = index; i < size_ - 1; i++) {
  48.             arr_[i] = arr_[i + 1];
  49.         }
  50.         size_--;
  51.         total_number_elements_--;
  52.     }
  53.  
  54.     int get_value(const int index) {
  55.         if (index >= 0 && index < size_) {
  56.             return arr_[index];
  57.         }
  58.     }
  59.  
  60.     void set_value(const int index, const int value) {
  61.         if (index < 0 || index >= size_) {
  62.             cout << "Invalid index" << endl;
  63.             return;
  64.         }
  65.         arr_[index] = value;
  66.     }
  67.  
  68.     void pop_back() {
  69.         size_--;
  70.         total_number_elements_--;
  71.     }
  72.  
  73.     void print() {
  74.         for (int i = 0; i < size_; i++) {
  75.             cout << arr_[i] << " ";
  76.         }
  77.         cout << endl;
  78.     }
  79.  
  80.     int size() {
  81.         return size_;
  82.     }
  83.  
  84.     int capacity() {
  85.         return capacity_;
  86.     }
  87.  
  88.     ~Vector() {
  89.         delete[] arr_;
  90.         total_number_elements_ -= size_;
  91.     }
  92.  
  93.     static int get_total_number_elements() {
  94.         return total_number_elements_;
  95.     }
  96.  
  97. private:
  98.     int* arr_; // массив для хранения элементов
  99.     int size_; // текущее количество элементов в массиве
  100.     int capacity_; // вместимость (емкость) массива
  101.     static int total_number_elements_; // общее количество элементов всех экземпляров класса Vector
  102.  
  103.     void check_capacity_() {
  104.         if (size_ == capacity_) {
  105.             int* temp = new int[capacity_ * 2];
  106.             for (int i = 0; i < size_; i++) {
  107.                 temp[i] = arr_[i];
  108.             }
  109.             delete[] arr_;
  110.             arr_ = temp;
  111.             capacity_ *= 2;
  112.         }
  113.     }
  114. };
  115.  
  116. void Print(vector<int>& vec) {
  117.     for (int a : vec) {
  118.         cout << a << " ";
  119.     }
  120.     cout << endl;
  121. }
  122.  
  123. int Vector::total_number_elements_ = 0;
  124.  
  125.  
  126. int main() {
  127.     setlocale(LC_ALL, "ru");
  128.  
  129.     Vector vec1;
  130.     for (int i = 0; i < 10; i++) {
  131.         vec1.push_back(i + 1);
  132.     }
  133.     vec1.print();
  134.  
  135.     vec1.pop_back();
  136.  
  137.     Vector vec2;
  138.     vec2.print();
  139.     vec2.push_back(35);
  140.     vec2.print();
  141.  
  142.     for (int i = 0; i < 10; i++) {
  143.         vec2.push_back(i + 1);
  144.     }
  145.     vec2.print();
  146.  
  147.     vec2.pop_back();
  148.     vec2.print();
  149.     vec2.insert(2, 55);
  150.     vec2.print();
  151.     vec2.erase(3);
  152.     vec2.print();
  153.     cout << vec2.get_value(3) << endl;
  154.     vec2.set_value(3, 33);
  155.     vec2.print();
  156.  
  157.     cout << "Элементов в векторе: " << vec1.size() << " Емкость вектора: " << vec1.capacity() << endl;
  158.  
  159.  
  160.     cout << "Элементов в векторе: " << vec2.size() << " Емкость вектора: " << vec2.capacity() << endl;
  161.  
  162.     Vector vec3(100);
  163.  
  164.     vec3.push_back(10);
  165.  
  166.     cout << "Элементов в векторе: " << vec3.size() << " Емкость вектора: " << vec3.capacity() << endl;
  167.  
  168.     Vector vec4(vec1);
  169.  
  170.     cout << "Элементов в векторе: " << vec4.size() << " Емкость вектора: " << vec4.capacity() << endl;
  171.  
  172.     vec1.push_back(22);
  173.  
  174.     vec1.print();
  175.     vec4.print();
  176.  
  177.     cout << Vector::get_total_number_elements() << endl;
  178.  
  179. }
  180.  
  181.  
Advertisement
Add Comment
Please, Sign In to add comment