// UBVector.cpp #include #include #include #include "UBVector.h" using namespace std; // actually allocate the const member const size_t UBVector::INITIAL_CAPACITY = 10; /** * ----------------------------------------------------------------------------- * constructor which contains a default constructor * ----------------------------------------------------------------------------- */ UBVector::UBVector(size_t n) : num_items(n), current_capacity(max(n, UBVector::INITIAL_CAPACITY)), item_ptr(new string[max(n, UBVector::INITIAL_CAPACITY)]) { /* constructor has empty body */ } /** * ----------------------------------------------------------------------------- * deep copy constructor * - copy the members other than the pointer * - allocate space for the pointer * - copy items one by one * ----------------------------------------------------------------------------- */ UBVector::UBVector(const UBVector& the_other) : num_items(the_other.num_items), current_capacity(the_other.num_items), item_ptr(new string[the_other.num_items]) { for (size_t i=0; iposition; --i) item_ptr[i] = item_ptr[i-1]; item_ptr[position] = new_item; ++num_items; } /** * ----------------------------------------------------------------------------- * reserve space so that the new vector can store at least n items * if n is less than twice the current capacity, double the current capacity * ----------------------------------------------------------------------------- */ void UBVector::reserve(size_t n) { if (n > current_capacity) { current_capacity = max(n, 2*current_capacity); string *new_data_ptr = new string[current_capacity]; for (size_t i=0; i