Advertisement
plarmi

workcpp_12_1

Jun 26th, 2023
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.58 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3.  
  4. template <typename T>
  5. class Array {
  6. private:
  7.     T* data;
  8.     int size;
  9.     int capacity;
  10.     int grow;
  11.  
  12. public:
  13.     Array() : data(nullptr), size(0), capacity(0), grow(0) {}
  14.  
  15.     ~Array() {
  16.         delete[] data;
  17.     }
  18.  
  19.     int GetSize() const {
  20.         return size;
  21.     }
  22.  
  23.     void SetSize(int newSize, int newGrow = 1) {
  24.         if (newSize > capacity) {
  25.             int newCapacity = capacity + newGrow;
  26.             T* newData = new T[newCapacity];
  27.             for (int i = 0; i < size; ++i) {
  28.                 newData[i] = data[i];
  29.             }
  30.             delete[] data;
  31.             data = newData;
  32.             capacity = newCapacity;
  33.         }
  34.         size = newSize;
  35.     }
  36.  
  37.     int GetUpperBound() const {
  38.         return size - 1;
  39.     }
  40.  
  41.     bool IsEmpty() const {
  42.         return size == 0;
  43.     }
  44.  
  45.     void FreeExtra() {
  46.         if (size < capacity) {
  47.             T* newData = new T[size];
  48.             for (int i = 0; i < size; ++i) {
  49.                 newData[i] = data[i];
  50.             }
  51.             delete[] data;
  52.             data = newData;
  53.             capacity = size;
  54.         }
  55.     }
  56.  
  57.     void RemoveAll() {
  58.         delete[] data;
  59.         data = nullptr;
  60.         size = 0;
  61.         capacity = 0;
  62.     }
  63.  
  64.     const T& GetAt(int index) const {
  65.         assert(index >= 0 && index < size);
  66.         return data[index];
  67.     }
  68.  
  69.     void SetAt(int index, const T& value) {
  70.         assert(index >= 0 && index < size);
  71.         data[index] = value;
  72.     }
  73.  
  74.     const T& operator[](int index) const {
  75.         return GetAt(index);
  76.     }
  77.  
  78.     T& operator[](int index) {
  79.         return data[index];
  80.     }
  81.  
  82.     void Add(const T& value) {
  83.         if (size == capacity) {
  84.             SetSize(size + grow);
  85.         }
  86.         data[size] = value;
  87.         size++;
  88.     }
  89.  
  90.     void Append(const Array<T>& otherArray) {
  91.         int otherSize = otherArray.GetSize();
  92.         if (otherSize > 0) {
  93.             int newSize = size + otherSize;
  94.             SetSize(newSize);
  95.             for (int i = 0; i < otherSize; ++i) {
  96.                 data[size + i] = otherArray[i];
  97.             }
  98.         }
  99.     }
  100.  
  101.     Array<T>& operator=(const Array<T>& otherArray) {
  102.         if (this != &otherArray) {
  103.             delete[] data;
  104.             size = otherArray.GetSize();
  105.             capacity = size;
  106.             data = new T[capacity];
  107.             for (int i = 0; i < size; ++i) {
  108.                 data[i] = otherArray[i];
  109.             }
  110.         }
  111.         return *this;
  112.     }
  113.  
  114.     T* GetData() const {
  115.         return data;
  116.     }
  117.  
  118.     void InsertAt(int index, const T& value) {
  119.         assert(index >= 0 && index <= size);
  120.         if (size == capacity) {
  121.             SetSize(size + grow);
  122.         }
  123.         for (int i = size; i > index; --i) {
  124.             data[i] = data[i - 1];
  125.         }
  126.         data[index] = value;
  127.         size++;
  128.     }
  129.  
  130.     void RemoveAt(int index) {
  131.         assert(index >= 0 && index < size);
  132.         for (int i = index; i < size - 1; ++i) {
  133.             data[i] = data[i + 1];
  134.         }
  135.         size--;
  136.     }
  137. };
  138.  
  139. int main() {
  140.     // Пример использования класса Array
  141.  
  142.     // Создание и инициализация массива
  143.     Array<int> myArray;
  144.     myArray.SetSize(5, 5);
  145.     for (int i = 0; i < myArray.GetSize(); ++i) {
  146.         myArray[i] = i;
  147.     }
  148.  
  149.     // Вывод элементов массива
  150.     for (int i = 0; i < myArray.GetSize(); ++i) {
  151.         std::cout << myArray[i] << " ";
  152.     }
  153.     std::cout << std::endl;
  154.  
  155.     // Изменение размера массива и добавление элементов
  156.     myArray.SetSize(8, 3);
  157.     for (int i = 5; i < myArray.GetSize(); ++i) {
  158.         myArray[i] = i;
  159.     }
  160.  
  161.     // Вывод элементов массива
  162.     for (int i = 0; i < myArray.GetSize(); ++i) {
  163.         std::cout << myArray[i] << " ";
  164.     }
  165.     std::cout << std::endl;
  166.  
  167.     // Получение последнего допустимого индекса
  168.     int upperBound = myArray.GetUpperBound();
  169.     std::cout << "Последний допустимый индекс: " << upperBound << std::endl;
  170.  
  171.     // Проверка, является ли массив пустым
  172.     bool empty = myArray.IsEmpty();
  173.     std::cout << "Массив пустой? " << (empty ? "Да" : "Нет") << std::endl;
  174.  
  175.     // Удаление "лишней" памяти
  176.     myArray.FreeExtra();
  177.  
  178.     // Удаление всех элементов массива
  179.     myArray.RemoveAll();
  180.  
  181.     return 0;
  182. }
  183.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement