Code: Show/Hide template <class _AC> class SVector
{ _AC **array; bool allocated; int count, space; public: SVector() { count = space = 0; array = NULL; allocated = false; } SVector(SVector &source) { count = source.count; space = source.space; array = copy(source.array, false); } ~SVector() { clear(); } void clear() { if (allocated) { for (int i = 0; i < count; i++) delete array[i]; delete [] array; allocated = false; } count = space = 0; array = NULL; } _AC *at(int offset) { if (offset > count) return NULL; return array[offset]; } _AC **copy(_AC **source, bool clear) { _AC **new_array = new _AC*[space]; allocated = true; for (int i = 0; i < count; i++) { _AC *item = array[i]; new_array[i] = new _AC(*item); if (clear) delete item; } if (clear) delete [] source; return new_array; } void append(_AC *item) { if (!allocated) allocate(1); if (space > count) array[count++] = new _AC(*item); else { space = ++count + EXTRA; array = copy(array, true); } } void allocate(int howmany) { space = howmany + EXTRA; if (allocated) { array = copy(array, true); } else { array = new _AC*[space]; allocated = true; count = 0; } } int size() { return count; } }; |