0001 template <typename T> void Vector<T>::expand() { //向量空间不足时扩容 0002 if ( _size < _capacity ) return; //尚未满员时,不必扩容 0003 if ( _capacity < DEFAULT_CAPACITY ) _capacity = DEFAULT_CAPACITY; //不低于最小容量 0004 T* oldElem = _elem; _elem = new T[_capacity <<= 1]; //容量加倍 0005 for ( int i = 0; i < _size; i++ ) 0006 _elem[i] = oldElem[i]; //复制原向量内容(T为基本类型,或已重载赋值操作符'=') 0007 delete [] oldElem; //释放原空间 0008 }