Advertisement
35657

Untitled

Feb 13th, 2024 (edited)
1,119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4.  
  5. using namespace std;
  6.  
  7. class Vector {
  8.  
  9. public:
  10.     Vector() {
  11.         arr_ = new int[4];
  12.         size_ = 0;
  13.         capacity_ = 4;
  14.     }
  15.  
  16.     Vector(const int capacity) {
  17.         arr_ = new int[capacity];
  18.         size_ = 0;
  19.         capacity_ = capacity;
  20.     }
  21.  
  22.     void push_back(const int value) {
  23.         check_capacity();
  24.         arr_[size_] = value;
  25.         size_++;
  26.     }
  27.  
  28.     void insert(const int index, const int value) {
  29.         if (index < 0 || index > size_) {
  30.             cout << "Некорректный индекс" << 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.     }
  40.  
  41.     void erase(const int index) {
  42.         // удаление элемента по указанному индексу
  43.     }
  44.  
  45.     void set_value(const int index, const int value) {
  46.         // изменяет значение элемента по указанному индексу на value
  47.     }
  48.  
  49.     int get_value(const int index) {
  50.         // возвращает значение элемента по указанному индексу
  51.     }
  52.  
  53.     void pop_back() {
  54.         if (size_ > 0) {
  55.             size_--;
  56.         }
  57.     }
  58.  
  59.     void print() {
  60.         for (int i = 0; i < size_; i++) {
  61.             cout << arr_[i] << " ";
  62.         }
  63.         cout << endl;
  64.     }
  65.  
  66. private:
  67.  
  68.     void check_capacity() {
  69.         if (size_ == capacity_) {
  70.             int* temp = new int[capacity_ * 2];
  71.             for (int i = 0; i < size_; i++) {
  72.                 temp[i] = arr_[i];
  73.             }
  74.             delete[] arr_;
  75.             arr_ = temp;
  76.             capacity_ *= 2;
  77.         }
  78.     }
  79.  
  80.     int* arr_; // хранилище
  81.     int size_; // текущее количество элементов
  82.     int capacity_; // емкость хранилища
  83. };
  84.  
  85. int main() {
  86.     setlocale(LC_ALL, "ru");
  87.  
  88.     Vector vec1;
  89.    
  90.     vec1.push_back(10);
  91.     vec1.push_back(15);
  92.     vec1.push_back(20);
  93.     vec1.push_back(25);
  94.  
  95.     vec1.print();
  96.  
  97.     vec1.pop_back();
  98.     vec1.print();
  99.  
  100.     vec1.insert(2, 44);
  101.     vec1.print();
  102.  
  103. }
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement