1

I'm creating a custom vector class as part of a homework assignment. What I am currently trying to do is implement a function called erase, which will take an integer as an argument, decrease my array length by 1, remove the element at the position specified by the argument, and finally shift all the elements down to fill in the gap left by "erased" element.

What I am not completely understanding, due to my lack of experience with this language, is how you can delete a single element from an array of pointers.

Currently, I have the following implemented:

        void myvector::erase(int i)
        {

            if(i != max_size)
            {
                for(int x = i; x < max_size; x++)
                {
                    vec_array[x] = vec_array[x+1];
                }
                vec_size --;

                //delete element from vector;
            }
            else
                //delete element from vector
        }

The class declaration and constructors look like this:

template <typename T> 

class myvector
{
    private:
            T *vec_array;
            int vec_size;
            int max_size;
            bool is_empty;

    public:
            myvector::myvector(int max_size_input)
            {
                max_size = max_size_input;
                vec_array = new T[max_size];
                vec_size = 0;
            }

I have tried the following:

  1. Using delete to try and delete an element

    delete vec_size[max_size];

    vec_size[max_size] = NULL;

  2. Setting the value of the element to NULL or 0

    vec_size[max_size] = NULL

or

vec_size[max_size] = 0

None of which are working for me due to either operator "=" being ambiguous or specified type not being able to be cast to void *.

I'm probably missing something simple, but I just can't seem to get passed this. Any help would be much appreciated. Again, sorry for the lack of experience if this is something silly.

3
  • 1
    You are not using an array of pointers there, you are using an array of objects. As such, you don't need to delete anything - just overwrite the element and shrink the vector. Also, call delete[] vec_array in your object's destructor, and it looks like you've got a off-by-one error in your size logic, too. Commented Dec 11, 2012 at 1:59
  • Yep. You are correct in the size error. Thank you. Edited. Commented Dec 11, 2012 at 2:17
  • @Johnny It seems you're new to SO (this is your first question.) Remember to upvote answers that helped you and accept the answer that helped you most. Commented Dec 11, 2012 at 2:44

2 Answers 2

2

If your custom vector class is supposed to work like std::vector, then don't concern yourself with object destruction. If you need to erase an element, you simply copy all elements following it by one position to the left:

void myvector::erase(int i)
{
    for (int x = i + 1; x < vec_size; x++) {
        vec_array[x - 1] = vec_array[x];
    }
    vec_size--;
}

That's all the basic work your erase() function has to do.

If the elements happen to be pointers, you shouldn't care; the user of your vector class is responsible for deleting those pointers if that's needed. You cannot determine if they can actually be deleted (the pointers might point to automatic stack variables, which are not deletable.)

So, do not ever call delete on an element of your vector.

If your vector class has a clear() function, and you want to make sure the elements are destructed, simply:

delete[] vec_array;
vec_array = new T[max_size];
vec_size = 0;

And this is how std::vector works, actually. (Well, the basic logic of it; of course you can optimize a hell of a lot of stuff in a vector implementation.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much for this answer. This is exactly what I was looking for. I think I was trying to complicate things beyond the range of this assignment.
@Johnny Then I guess that the assignment helped teach you a very important lesson. And I don't mean how to code a vector... Good luck with future assignments!
1

Since this is homework i wont give you a definitive solution, but here is one method of erasing a value:

loop through and find value specified in erase function
mark values position in the array
starting from that position, move all elements values to the previous element(overlapping 'erased' value)
for i starting at position, i less than size minus one, i plus plus
    element equals next element 
reduce size of vector by 1

see if this is a big enough hint.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.