#include #include IntArrayList::IntArrayList() : capacity_(10), len_(0) { arr_ = new int[capacity_]; } IntArrayList::IntArrayList(int capacity) : capacity_(capacity), len_(0) { arr_ = new int[capacity_]; } IntArrayList::IntArrayList(const IntArrayList& rhs) : capacity_(rhs.capacity_), len_(rhs.len_) { arr_ = new int[capacity_]; memcpy(arr_, rhs.arr_, len_); } IntArrayList::IntArrayList(const int* arr, int len) : capacity_(len * 2), len_(len) { arr_ = new int[capacity_]; memcpy(arr_, arr, len); } IntArrayList::~IntArrayList() { delete [] arr_; } size_t IntArrayList::len() { return len_; } IntArrayList& IntArrayList::operator=(const IntArrayList& rhs) { if (this != &rhs) { len_ = rhs.len_; capacity_ = rhs.capacity_; delete [] arr_; arr_ = new int[capacity_]; memcpy(arr_, rhs.arr_, len_); } return *this; } void IntArrayList::resize(size_t cap) { int* tmp = new int[cap]; memcpy(tmp, arr_, len_); delete [] arr_; arr_ = tmp; capacity_ = cap; } void IntArrayList::operator+=(const IntArrayList& other) { if (len_ + other.len_ > capacity_) { resize((len_ + other.len_) * 2); } memcpy(arr_ + len_, other.arr_, other.len_); len_ += other.len_; } void IntArrayList::operator+=(int n) { if (len_ + 1 > capacity_) { resize((len_ + 1) * 2); } arr_[len_] = n; len_++; } int& IntArrayList::operator[](int indx) { if (indx < 0 || indx >= len_) { throw; } return arr_[indx]; } friend std::ostream& operator<<(std::ostream& ostr, IntArrayList& rhs) { ostr << "["; for (size_t i = 0; i < rhs.len_ - 1; i++) { ostr << rhs.arr_ << ", "; } if (rhs.len_ > 0) ostr << rhs.arr_[rhs.len_ - 1]; ostr << "]"; return ostr; }