#include template class DynamicArray { T *values; int lastUnused; int allocated; void free(); void copyFrom(const DynamicArray& other); void resize(); public: DynamicArray(); ~DynamicArray(); DynamicArray(const DynamicArray& other); DynamicArray& operator=(const DynamicArray& other); DynamicArray(DynamicArray&& other); DynamicArray& operator=(DynamicArray&& other); void Add(const T& newValue); // Тези оператори променят аргумента си, затова е по-добре да са член-функции DynamicArray& operator+=(const DynamicArray& other); DynamicArray& operator+=(const T& newValue); // По дефиниция не може operator=, operator(), operator[] и operator-> да бъдат имплементирани // като приятелски функции T& operator[](int index); // Тези оператори не променят аргументите си, затова е по-добре да са приятелски template friend DynamicArray operator+(const DynamicArray& left, const DynamicArray& right); template friend DynamicArray operator+(const DynamicArray& left, const U& right); template friend bool operator==(const DynamicArray& left, const DynamicArray& right); template friend bool operator!=(const DynamicArray& left, const DynamicArray& right); template friend std::ostream& operator<<(std::ostream& ostr, const DynamicArray& right); template friend std::istream& operator>>(std::istream& ostr, DynamicArray& right); }; template void DynamicArray::free() { delete[] values; } template void DynamicArray::copyFrom(const DynamicArray& other) { this->lastUnused = other.lastUnused; this->allocated = other.allocated; this->values = new T[allocated]; for (int i = 0; i < this->lastUnused; i++) { this->values[i] = other.values[i]; } } template void DynamicArray::resize() { allocated *= 2; T* moreValues = new T[allocated]; for (int i = 0; i < lastUnused; i++) { moreValues[i] = values[i]; } delete[] values; values = moreValues; } template DynamicArray::DynamicArray() { values = nullptr; lastUnused = allocated = 0; } template DynamicArray::~DynamicArray() { free(); } template DynamicArray::DynamicArray(const DynamicArray& other) { copyFrom(other); } template DynamicArray& DynamicArray::operator=(const DynamicArray& other) { if (this != &other) { free(); copyFrom(other); } return *this; } template DynamicArray::DynamicArray(DynamicArray&& other) { this->values = other.values; other.values = nullptr; this->lastUnused = other.lastUnused; this->allocated = other.allocated; } template DynamicArray& DynamicArray::operator=(DynamicArray&& other) { if (this != &other) { free(); this->values = other.values; other.values = nullptr; this->lastUnused = other.lastUnused; this->allocated = other.allocated; } return *this; } template void DynamicArray::Add(const T& number) { if (allocated == lastUnused) { resize(); } values[lastUnused++] = number; } template DynamicArray& DynamicArray::operator+=(const DynamicArray& other) { for (int i = 0; i < other.lastUnused; i++) { this->Add(other.values[i]); } return *this; } template DynamicArray& DynamicArray::operator+=(const T& number) { for (int i = 0; i < lastUnused; i++) { values[i] += number; } return *this; } template T& DynamicArray::operator[](int index) { if (index < 0 || lastUnused <= index) { throw "Invalid index!"; } return values[index]; } template DynamicArray operator+(const DynamicArray& left, const DynamicArray& right) { DynamicArray output; (output += left) += right; return output; } template DynamicArray operator+(const DynamicArray& left, T right) { DynamicArray output = left; // Копиращ конструктор output += right; return output; } template bool operator==(const DynamicArray& left, const DynamicArray& right) { if (left.lastUnused != right.lastUnused) { return false; } for (int i = 0; i < left.lastUnused; i++) { if (left.values[i] != right.values[i]) { return false; } } return true; } template bool operator!=(const DynamicArray& left, const DynamicArray& right) { return !(left == right); } template std::ostream& operator<<(std::ostream& ostr, const DynamicArray& right) { for (int i = 0; i < right.lastUnused; i++) { ostr << right.values[i] << " "; } return ostr; } template std::istream& operator>>(std::istream& istr, DynamicArray& right) { int length; istr >> length; right.lastUnused = 0; for (int i = 0; i < length; i++) { T temp; istr >> temp; right.Add(temp); } return istr; }