Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- template <typename T>
- class Array {
- private:
- T* data;
- int size;
- int capacity;
- int grow;
- public:
- Array() : data(nullptr), size(0), capacity(0), grow(0) {}
- ~Array() {
- delete[] data;
- }
- int GetSize() const {
- return size;
- }
- void SetSize(int newSize, int newGrow = 1) {
- if (newSize > capacity) {
- int newCapacity = capacity + newGrow;
- T* newData = new T[newCapacity];
- for (int i = 0; i < size; ++i) {
- newData[i] = data[i];
- }
- delete[] data;
- data = newData;
- capacity = newCapacity;
- }
- size = newSize;
- }
- int GetUpperBound() const {
- return size - 1;
- }
- bool IsEmpty() const {
- return size == 0;
- }
- void FreeExtra() {
- if (size < capacity) {
- T* newData = new T[size];
- for (int i = 0; i < size; ++i) {
- newData[i] = data[i];
- }
- delete[] data;
- data = newData;
- capacity = size;
- }
- }
- void RemoveAll() {
- delete[] data;
- data = nullptr;
- size = 0;
- capacity = 0;
- }
- const T& GetAt(int index) const {
- assert(index >= 0 && index < size);
- return data[index];
- }
- void SetAt(int index, const T& value) {
- assert(index >= 0 && index < size);
- data[index] = value;
- }
- const T& operator[](int index) const {
- return GetAt(index);
- }
- T& operator[](int index) {
- return data[index];
- }
- void Add(const T& value) {
- if (size == capacity) {
- SetSize(size + grow);
- }
- data[size] = value;
- size++;
- }
- void Append(const Array<T>& otherArray) {
- int otherSize = otherArray.GetSize();
- if (otherSize > 0) {
- int newSize = size + otherSize;
- SetSize(newSize);
- for (int i = 0; i < otherSize; ++i) {
- data[size + i] = otherArray[i];
- }
- }
- }
- Array<T>& operator=(const Array<T>& otherArray) {
- if (this != &otherArray) {
- delete[] data;
- size = otherArray.GetSize();
- capacity = size;
- data = new T[capacity];
- for (int i = 0; i < size; ++i) {
- data[i] = otherArray[i];
- }
- }
- return *this;
- }
- T* GetData() const {
- return data;
- }
- void InsertAt(int index, const T& value) {
- assert(index >= 0 && index <= size);
- if (size == capacity) {
- SetSize(size + grow);
- }
- for (int i = size; i > index; --i) {
- data[i] = data[i - 1];
- }
- data[index] = value;
- size++;
- }
- void RemoveAt(int index) {
- assert(index >= 0 && index < size);
- for (int i = index; i < size - 1; ++i) {
- data[i] = data[i + 1];
- }
- size--;
- }
- };
- int main() {
- // Пример использования класса Array
- // Создание и инициализация массива
- Array<int> myArray;
- myArray.SetSize(5, 5);
- for (int i = 0; i < myArray.GetSize(); ++i) {
- myArray[i] = i;
- }
- // Вывод элементов массива
- for (int i = 0; i < myArray.GetSize(); ++i) {
- std::cout << myArray[i] << " ";
- }
- std::cout << std::endl;
- // Изменение размера массива и добавление элементов
- myArray.SetSize(8, 3);
- for (int i = 5; i < myArray.GetSize(); ++i) {
- myArray[i] = i;
- }
- // Вывод элементов массива
- for (int i = 0; i < myArray.GetSize(); ++i) {
- std::cout << myArray[i] << " ";
- }
- std::cout << std::endl;
- // Получение последнего допустимого индекса
- int upperBound = myArray.GetUpperBound();
- std::cout << "Последний допустимый индекс: " << upperBound << std::endl;
- // Проверка, является ли массив пустым
- bool empty = myArray.IsEmpty();
- std::cout << "Массив пустой? " << (empty ? "Да" : "Нет") << std::endl;
- // Удаление "лишней" памяти
- myArray.FreeExtra();
- // Удаление всех элементов массива
- myArray.RemoveAll();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement