Unable to match functions definition

Pages: 12
closed account (SECMoG1T)
Hey does this work

1
2
numElements--;/// well this just decrements the number of elements,  it doesn't do the real popping 
                              ///don't you think so 


For this case , popping your elements would be a very costly process, copying ,deleting , and creating a new array ..just for the purpose of deleting a single elements is a really big deal, I would advice you to switch to linked lists, they just involve manipulating several pointers , don't you think that is a bit reasonable .
I completely agree with you, this is what is required from the text. So I need a way to just use pop_back in this form, linked lists are in the next chapter of my text.

The function
void pop_back().
This function removes the last integer pushed into the vector. The size of the vector is reduced by one. The function does nothing if the vector is empty.
The function
int last().
This function returns the value of the last integer pushed into the vector, but it does not remove it. Do not worry about the case where the vector is empty.
The function
operator[](int).
This function overloads the [ ] operator we use to index into an array. If you need help understanding how to do this, review the chapter on operator overloading in Big C++.
closed account (SECMoG1T)
so will settle for arrays as in your current code or will you switch to linked list.

If you insist to stick to arrays you would have to:-
1. Create a dynamic array with size less than one.
2. Copy all elements ignoring the last one.
3. Decrement the element counter bu one
4. Delete the old array.

That's all you'll need.
Last edited on
Thanks for laying that out for me, I am not certain how to start with something like that within my template implementation for pop_back. I will continue research, if you have any other suggestions please let me know.
closed account (SECMoG1T)
If the function just removes the last element. . Then here you go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 template <typename T> 
 void myVector<T>::pop_back()
 { 
 	if (!myVector.empty()) /// you'll have defined this already which return (cap==0&&numElements==0)
 	{ 	
            T* temp=new T [numElements-1];///temporal array
             
            for (size_t i=0; i <(numElements-1); i++)
              {
                   temp [i]=vectorData [i];
               }
            
             delete [] vectorData;
             vectorData=temp;
             numElements--;
 	} 
}



That's the code , I also noted your copy constructor doesn't take care or cap and numElements.
Last edited on
Awesome thank you for your help!
closed account (SECMoG1T)
Welcome XD.
Topic archived. No new replies allowed.
Pages: 12