35657

Untitled

Feb 17th, 2024
837
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <iostream>
  2.  
  3.  
  4. using namespace std;
  5.  
  6. class Vector {
  7.  
  8. public:
  9.     Vector() {
  10.         arr_ = new int[4];
  11.         size_ = 0;
  12.         capacity_ = 4;
  13.     }
  14.  
  15.     Vector(const int capacity) {
  16.         arr_ = new int[capacity];
  17.         size_ = 0;
  18.         capacity_ = capacity;
  19.     }
  20.  
  21.     void push_back(const int value) {
  22.         check_capacity();
  23.         arr_[size_] = value;
  24.         size_++;
  25.     }
  26.  
  27.     void pop_back() {
  28.         if (size_ > 0) {
  29.             size_--;
  30.         }
  31.     }
  32.  
  33.     void insert(const int index, const int value) {
  34.         if (index < 0 || index > size_) {
  35.             cout << "Некорректный индекс" << endl;
  36.             return;
  37.         }
  38.         check_capacity();
  39.         for (int i = size_; i > index; i--) {
  40.             arr_[i] = arr_[i - 1];
  41.         }
  42.         arr_[index] = value;
  43.         size_++;
  44.     }
  45.  
  46.     void erase(const int index) {
  47.         if (index < 0 || index >= size_) {
  48.             cout << "Некорректный индекс" << endl;
  49.             return;
  50.         }
  51.         for (int i = index; i < size_ - 1; i++) {
  52.             arr_[i] = arr_[i + 1];
  53.         }
  54.         size_--;
  55.     }
  56.  
  57.     void set_value(const int index, const int value) {
  58.         if (index < 0 || index >= size_) {
  59.             cout << "Некорректный индекс" << endl;
  60.             return;
  61.         }
  62.         arr_[index] = value;
  63.     }
  64.  
  65.     int get_value(const int index) {
  66.         if (index < 0 || index >= size_) {
  67.             cout << "Некорректный индекс" << endl;
  68.             // здесь функция возвращает int поэтому выйти по return не получится, использовать abort() можно, но тоже не лучший выход, пока пусть будет так, потом обработаем эту ситуацию с помощью исключений
  69.         }
  70.         return arr_[index];
  71.     }
  72.  
  73.     int size() {
  74.         return size_;
  75.     }
  76.  
  77.     int capacity() {
  78.         return capacity_;
  79.     }
  80.  
  81.     void print() {
  82.         for (int i = 0; i < size_; i++) {
  83.             cout << arr_[i] << " ";
  84.         }
  85.         cout << endl;
  86.     }
  87.  
  88. private:
  89.  
  90.     void check_capacity() {
  91.         if (size_ == capacity_) {
  92.             int* temp = new int[capacity_ * 2];
  93.             for (int i = 0; i < size_; i++) {
  94.                 temp[i] = arr_[i];
  95.             }
  96.             delete[] arr_;
  97.             arr_ = temp;
  98.             capacity_ *= 2;
  99.         }
  100.     }
  101.  
  102.     int* arr_; // хранилище
  103.     int size_; // текущее количество элементов
  104.     int capacity_; // емкость хранилища
  105. };
  106.  
  107. int main() {
  108.     setlocale(LC_ALL, "ru");
  109.  
  110.     Vector vec1;
  111.  
  112.     vec1.push_back(10);
  113.     vec1.push_back(15);
  114.     vec1.push_back(20);
  115.     vec1.push_back(25);
  116.     vec1.push_back(22);
  117.  
  118.     vec1.print();
  119.  
  120.     vec1.pop_back();
  121.     vec1.print();
  122.  
  123.     vec1.insert(2, 44);
  124.     vec1.print();
  125.  
  126.     vec1.erase(2);
  127.     vec1.print();
  128.  
  129.     vec1.set_value(0, 33);
  130.     cout << vec1.get_value(3) << endl;
  131.  
  132.  
  133.     vec1.print();
  134.  
  135.     cout << vec1.size() << endl;
  136.     cout << vec1.capacity() << endl;
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment