#include "Vector.h" void Vector::resize() { capacity *= 2; int* newArr = new int[capacity]; for (size_t i = 0; i < size; i++) { newArr[i] = arr[i]; } free(); arr = newArr; } void Vector::free() { delete[] arr; } void Vector::copyFrom(const Vector& other) { size = other.size; capacity = other.capacity; arr = new int[capacity]; for (int i = 0; i < size; i++) { arr[i] = other.arr[i]; } } void Vector::swap(const int ind1, const int ind2) { int temp = arr[ind1]; arr[ind1] = arr[ind2]; arr[ind2] = temp; } Vector::Vector() { arr = new int[2]; capacity = 2; size = 0; } Vector::Vector(const Vector& other) { copyFrom(other); } Vector& Vector::operator=(const Vector& other) { if (this != &other) { free(); copyFrom(other); } return *this; } Vector::~Vector() { free(); } void Vector::push_back(const int element) { if (size < capacity) { arr[size] = element; size++; } else { resize(); push_back(element); } } int Vector::getSize() const { return size; } int& Vector::operator[](const int index) { if (index >= 0 && index < size) { return arr[index]; } throw "Out of range exception"; } const int& Vector::operator[](int index) const { if (index >= 0 && index < size) { return arr[index]; } throw "Out of range exception"; } void Vector::deleteAt(const int index) { for (int i = index; i < size; i++) { arr[i] = arr[i + 1]; } pop_back(); } void Vector::pop_back() { arr[size--] = int(); } int Vector::findItem(const int item) const { for (int i = 0; i < size; i++) { if (arr[i] == item) return i; } return -1; } void Vector::clear() { delete[] arr; size = capacity = 0; } void Vector::sort() { for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (arr[i] > arr[j]) swap(i, j); } } } void Vector::reverse() { for (int halfSize = size / 2, offset = 0; offset < halfSize; offset++) swap(offset, size - offset - 1); } std::ostream& operator<<(std::ostream& ostr, const Vector& vec) { ostr << "<"; for (int i = 0; i < vec.getSize() - 2; i++) { ostr << vec[i] << ", "; } ostr << vec[vec.getSize() - 1] << ">"; return ostr; } std::istream& operator<<(std::istream& istr, const Vector& vec) { return istr; }