35657

Untitled

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