How can I make my resize
function include the elements of the previous vector? This is basically mimicking a vector
and I have created The push_back
and pop_back
functions.
I have also created a resize
function that doubles the size and a resize
function that will 1/2 the size. Is it possible to include the elements of the previous vector in the resized vector?
My created functions are resize
, push_back
, pop_off
, and copy
. I was able to copy the previous vector elements in the resize
function, however all of the other elements were like this: -192828272
, so it currently just sets the elements to zero.
See the function below.
//1/2 the array size
template <class T>
T SimpleVector<T>::resizeDwn( ){
// decrease the size
arraySize /= 2;
// Allocate memory for the array.
aptr = new T [arraySize];
if (aptr == 0)
memError();
// Set the elements to zero
for (int count = 0; count < arraySize; count++){
aptr[count] = 0;
}
// Return the array
return *aptr;
}
//Double the array size
template <class T>
T SimpleVector<T>::resizeUp( ){
// Increase the size
arraySize *= 2;
// Allocate memory for the array.
aptr = new T [arraySize];
if (aptr == 0)
memError();
// Set the elements to zero
for (int count = 0; count < arraySize; count++){
aptr[count] = 0;
}
return *aptr;
}