/* adds an element in the back of myVector */ voidpush_back(const T& t){ insert_after(theSize - 1, t); }
/* adds an element in the front of myVector */ voidpush_front(const T& t){ insert_before(0, t); }
/* inserts an element after the pos */ // the pos must be in [0, theSize] voidinsert_after(int pos, const T& t){ insert_before(pos + 1, t); }
/* inserts an element before the pos */ // the pos must be less than myVector.size() voidinsert_before(int pos, const T& t){ if (theSize == theCapacity) { resize(2 * theCapacity); } for (int i = (int)theSize - 1; i >= pos; --i) { array[i+1] = array[i]; } array[pos] = t; theSize++; }
/* erase an element in the pos*/ // pos must be in [0, theSize] voiderase(unsignedint pos){ if (pos < theSize) { for (unsignedint = pos+1; i < theSize; ++i) { array[i-1] = array[i]; } --theSize; } if (theSize == theCapacity / 4 && theCapacity / 2 != 0) { resize(theCapacity / 2); } }
/* 动态扩缩容 */ voidresize(int newCapacity){ T* newArray = new T[newCapacity]; for (int i = 0; i < theSize; ++i) { newArray[i] = array[i]; } array = newArray; theCapacity = newCapacity; newArray = nullptr; delete [] newArray; }