Advertisement
coloriot

HA23

Sep 11th, 2024
24
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. // Пишем класс векторов
  6.  
  7. template <typename T>
  8. class Vector {
  9.     T* array;
  10.     int size;
  11.     int capacity;
  12. public:
  13.  
  14.     // Конструктор инициилизации
  15.     Vector(){
  16.         array = nullptr;
  17.         size = 0;
  18.         capacity = 0;
  19.     }
  20.  
  21.     // Конструктор копирования
  22.     Vector(const Vector& other){
  23.         size = other.size;
  24.         capacity = other.capacity;
  25.         array = new T[capacity];
  26.         for (int i = 0; i < size; ++i){
  27.             array[i] = other.array[i];
  28.         }
  29.     }
  30.  
  31.     // Перегружаем равно
  32.     Vector& operator=(const Vector& other){
  33.         if (this != &other){
  34.             delete[] array;
  35.             size = other.size;
  36.             capacity = other.capacity;
  37.             array = new T[capacity];
  38.             for (int i=0; i < size; ++i){
  39.                 array[i] = other.array[i];
  40.             }
  41.         }
  42.         return *this;
  43.     }
  44.  
  45.     // Доступы:
  46.     T& operator[](int index){
  47.         return array[index];
  48.     }
  49.     const T& operator[](int index) const{
  50.         return array[index];
  51.     }
  52.     int getSize() const{
  53.         return size;
  54.     }
  55.     int getCapacity() const{
  56.         return capacity;
  57.     }
  58.  
  59.     // Push и Pop
  60.     void push_back(const T& value){
  61.         if (size == capacity){
  62.             if (capacity == 0){
  63.                 reserve(1);
  64.             } else {
  65.                 reserve (capacity * 2);
  66.             }
  67.         }
  68.         array[size] = value;
  69.         ++size;
  70.     }
  71.     void pop_back(){
  72.         if (size > 0){
  73.             --size;
  74.         }
  75.     }
  76.  
  77.     // Резервация - задаем новую вместимость
  78.     void reserve(int new_capacity){
  79.         if (new_capacity > capacity){
  80.             T* new_array = new T[new_capacity];
  81.             for (int i = 0; i < size; ++i){
  82.                 new_array[i] = array[i];
  83.             }
  84.             delete[] array;
  85.             array = new_array;
  86.             capacity = new_capacity;
  87.         }
  88.     }
  89.  
  90.     // Ресайз - добавляем значение
  91.     void resize(int new_size, const T& value){
  92.         if (new_size > capacity){
  93.             reserve(new_size);
  94.         }
  95.         for (int i = size; i < new_size; ++i){
  96.             array[i] = value;
  97.         }
  98.         size = new_size;
  99.     }
  100.     // Деструктор
  101.     ~Vector(){
  102.         delete[] array;
  103.     }
  104. };
  105.  
  106.  
  107. int main() {
  108.     Vector<int> vec;
  109.  
  110.     vec.push_back(1);
  111.     vec.push_back(2);
  112.     vec.push_back(3);
  113.  
  114.     cout << "Размер: " << vec.getSize() << endl;
  115.     cout << "Объем: " << vec.getCapacity() << endl;
  116.  
  117.     cout << "Элемент по индексу 0: " << vec[0] << endl;
  118.     cout << "Элемент по индексу 1: " << vec[1] << endl;
  119.     cout << "Элемент по индексу 0: " << vec[2] << endl;
  120.  
  121.     vec.pop_back();
  122.     cout << "Размер после pop_back: " << vec.getSize() << endl;
  123.  
  124.     vec.resize(5, 10);
  125.     cout << "Размер после ресайза: " << vec.getSize() << endl;
  126.     cout << "Элемент по индексу 3: " << vec[3] << endl;
  127.     cout << "Элемент по индексу 3: " << vec[4] << endl;
  128.  
  129.     return 0;
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement