Advertisement
35657

Untitled

Feb 13th, 2024
916
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.14 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.         if (size_ == capacity_) {
  24.             int* temp = new int[capacity_ * 2];
  25.             for (int i = 0; i < size_; i++) {
  26.                 temp[i] = arr_[i];
  27.             }
  28.             delete[] arr_;
  29.             arr_ = temp;
  30.             capacity_ *= 2;
  31.         }
  32.         arr_[size_] = value;
  33.         size_++;
  34.     }
  35.  
  36.     void insert(const int index, const int value) {
  37.  
  38.     }
  39.  
  40.     void pop_back() {
  41.         if (size_ > 0) {
  42.             size_--;
  43.         }
  44.     }
  45.  
  46.     void print() {
  47.         for (int i = 0; i < size_; i++) {
  48.             cout << arr_[i] << " ";
  49.         }
  50.         cout << endl;
  51.     }
  52.  
  53. private:
  54.     int* arr_; // хранилище
  55.     int size_; // текущее количество элементов
  56.     int capacity_; // емкость хранилища
  57. };
  58.  
  59. int main() {
  60.     setlocale(LC_ALL, "ru");
  61.  
  62.     Vector vec1;
  63.    
  64.     vec1.push_back(10);
  65.     vec1.push_back(15);
  66.     vec1.push_back(20);
  67.     vec1.push_back(25);
  68.  
  69.     vec1.print();
  70.  
  71.     vec1.pop_back();
  72.     vec1.print();
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement