简体   繁体   English

从指针数组中删除元素

[英]Deleting an element from an array of pointers

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. 我目前要做的是实现一个名为erase的函数,它将整数作为参数,将我的数组长度减少1,删除参数指定位置的元素,最后将所有元素向下移动到填充在“擦除”元素留下的空隙中。

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尝试删除元素

    delete vec_size[max_size]; 删除vec_size [max_size];

    vec_size[max_size] = NULL; vec_size [max_size] = NULL;

  2. Setting the value of the element to NULL or 0 将元素的值设置为NULL或0

    vec_size[max_size] = NULL 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 *. 由于操作符“=”不明确或指定的类型无法转换为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. 再次,抱歉缺乏经验,如果这是愚蠢的事情。

If your custom vector class is supposed to work like std::vector, then don't concern yourself with object destruction. 如果您的自定义向量类应该像std :: vector一样工作,那么不要关心对象破坏。 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. 这是你的erase()函数必须做的所有基本工作。

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. 如果需要,vector类的用户负责删除那些指针。 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. 所以,不要在向量的元素上调用delete

If your vector class has a clear() function, and you want to make sure the elements are destructed, simply: 如果你的vector类有一个clear()函数,并且你想确保元素被破坏,那么只需:

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

And this is how std::vector works, actually. 实际上,这就是std :: vector的工作原理。 (Well, the basic logic of it; of course you can optimize a hell of a lot of stuff in a vector implementation.) (好吧,它的基本逻辑;当然你可以在矢量实现中优化很多东西。)

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. 看看这是否是一个足够大的暗示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM