Logo Data Structures and Algorithms with Object-Oriented Design Patterns in C++
next up previous contents index

Resizing an Array

 

The definitions the SetBase and SetLength member functions of the Array<T> class are given in Program gif. The SetBase function simply modifies the base field as required. The SetLength function provides a means to change the size of an array at run time. This function can be used both to increase and to decrease the size of an array.

   program2812
Program: Array<T> Class SetBase and SetLength Member Function Definitions

The running time of this algorithm depends on two parameters. If n is the original size of the array and m is the new size of the array, then the running time of Resize is tex2html_wrap_inline60993. This is because the program first allocates and initializes a new array of size m; then it copies at most tex2html_wrap_inline60997 elements from the old array to the new array; and then it deletes the old array. And in the case where T is a built-in C++ data type, this simplifies to tex2html_wrap_inline60999.


next up previous contents index

Bruno Copyright © 1997 by Bruno R. Preiss, P.Eng. All rights reserved.