35657

Untitled

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