Code Fragment: vector2



// ...						// out-of-class definitions
template <typename Object>
void ArrayVector<Object>::
removeAtRank(int r) {				// remove element at given rank
  for (int i = r; i < sz-1; i++)
    a[i] = a[i+1];				// shift elements down
  sz--;
}

template <typename Object>
void ArrayVector<Object>::
insertAtRank(int r, const Object& e) {		// insert element at given rank
  if (sz == capacity) overflow();		// handle overflow
  for (int i = sz-1; i >= r; i--)
    a[i+1] = a[i];				// shift elements up
  a[r] = e;
  sz++;
}

template <typename Object>
void ArrayVector<Object>::
overflow() {					// handle overflow by resizing
  capacity *= 2;				// double capacity
  Object* b = new Object[capacity];
  for (int i = 0; i < sz; i++) {		// copy contents to new array
    b[i] = a[i];
  }
  delete [] a;					// discard old array
  a = b;					// make b the new array
}