-2

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;
}

Program output

kn0w0n3
  • 169
  • 1
  • 13
  • 2
    Why do you think it's important to dump this massive pile up of code, showing the details of every irrelevant method of the class, when your question has only to do with resizing? You need to edit your question and remove all irrelevant filler, leaving only the relevant parts. – Sam Varshavchik Sep 20 '16 at 02:31
  • Why are you shutting the program down (your `memError` function) if the constructor throws an exception? [Read this](http://stackoverflow.com/questions/22843189/exit-call-inside-a-function-which-should-return-a-reference/22843464#22843464). You're also missing an assignment operator. – PaulMcKenzie Sep 20 '16 at 03:05
  • As to your resize() function, where do you `delete[]` the old array? A typical `vector` class has two "sizes" -- the actual number of elements that can be accessed, and a "capacity" that determines the maximum number of elements that can be added before calling the resize() method. Your vector class lacks this, and thus is highly inefficient in addition to causing memory leaks. – PaulMcKenzie Sep 20 '16 at 03:13
  • You just need to copy the elements in your resize function. At the moment you just set the elements to zero. For some types this wont work. Note that as a result of using an Array of `T` inside your vector `T` must have a default (no argument) constructor. Writing generic containers is hard, but don't be put off. You should correct what you know to be wrong and try posting on the codereview stack exchange site. – Paul Rooney Sep 20 '16 at 03:17

1 Answers1

0

I have tried to perform what have you asked. No bound checking is added in this

template <class TT>
class SimpleVector {
    TT* arr;
    int size;
public:
    SimpleVector() {}

    SimpleVector(TT n) {
        this->arr = new TT[n];
        this->size = 0;
    }

    int getLength() {
        return this->size;
    }

    void resizeUp(SimpleVector old) { // Here I have modified your resizeUp declaration
        this->size = old.getLength() * 2;
        this->arr = new TT[pointer];
        /**
         * Previous two lines are allocating the
         * array with double the size of Old SimpleVector.
         */
        for(int i = 0; i < old.getLength(); i++)
            this->arr[i] = old.arr[i];
    }
};       

feel free to put question on this.

Parnab Sanyal
  • 749
  • 5
  • 19