【问题标题】:Creating a custom vector class. Push_back function only working for the first value创建自定义矢量类。 Push_back 函数仅适用于第一个值
【发布时间】:2021-02-22 17:41:34
【问题描述】:

在我的 Comp Sci 课程中,我们正在学习如何制作自己的矢量类。我们最终会将我们定制的字符串类对象存储在定制的向量类中。为了简单起见,我想尝试预先构建一个整数向量类。

到目前为止,我有一个默认构造函数,它将我的指针初始化为一个空数组并将大小设置为 0。然后我尝试使用我的 push_back 函数附加一些值,然后检查以确保它正确完成。

当我做 std::cout

我得到正确的输出 (10)。但是,如果我再次调用 push_back 然后调用 v[1] 我得到 0。

我觉得我在 push_back 函数中没有正确分配内存,但我不确定。

感谢您的建议!

[第 1 部分][1]

[第 2 部分][2]

对不起,如果我的格式有误,我是新来这里发帖的。

类:

class myVector
{
private:
    int *data; //will point to an array of ints
    size_t size; //determins the size of array
public:
    myVector(); // default constructor
    void push_back(int); // appends an integer to the vector
    int operator[](size_t);
    size_t sizeOf();
};

主要:

int main()
{
    myVector v;
    v.push_back(10);
    std::cout << v.sizeOf() << std::endl;
    v.push_back(14);
    std::cout << v.sizeOf() << std::endl;
    std::cout << v[1] << std::endl;

    return 0;

}

成员函数:

size_t myVector::sizeOf()
{
    return size;
}

int myVector::operator[](size_t location)
{
    return this->data[location]; //this will return the value at data + 
                                 //location
}

myVector::myVector()
{
    this->data = new int[0]; //initialize the data to an empty array of 
                             //ints
    size = 0; //initialize the size to 0
}

void myVector::push_back(int val)
{
    if(size == 0) //if size == 0, create a new array with 1 extra index
    {
        ++size;
        delete [] this->data;
        this->data = new int[size];
        this->data[0] = val;
    }
    else
    {
         ++size;
         int *temp = new int[size - 1];
         for(int i = 0; i != (size - 1); i++)
         {
              temp[i] = this->data[i];
         }
         delete [] this->data;
         this->data = new int[size];
         for(int i = 0; i != (size - 1); i++)
         {
             this->data[i] = temp[i];
         }
         this->data[size] = val;
         delete [] temp;
    }
}

【问题讨论】:

  • 请在问题中包含minimal reproducible example。请以文本而不是图像的形式编码
  • 我很抱歉,试图让格式正确到足以提交是很烦人的。但现在它起来了!
  • 你从size=0开始,然后你增加大小并通过new int[size -1]分配一个数组,对于1个元素来说仍然不够大。
  • @idclev463035818 这实际上不是问题,因为稍后会有 second 分配 this-&gt;data = new int[size]。在这段代码中,newdelete[] 的用法太多了,但实际上并没有导致这个特定问题。
  • @NathanPierson 哦,对了,谢谢。所看到的只是将元素存储在某个临时数组中(无论出于何种原因),我只是看到了,并没有继续阅读。

标签: c++ arrays class pointers dynamic-memory-allocation


【解决方案1】:

很多newdelete 操作和循环都没有必要。我修复并清理了你的两个函数。

myVector::myVector()
{
    this->data = new int[1]; //initialize the data to an empty array of 
                             //ints
    size = 0; //initialize the size to 0
}

void myVector::push_back(int val)
{
    if(size == 0) //if size == 0, create a new array with 1 extra index
    {
        ++size;
        this->data[0] = val;
    }
    else
    {
         ++size;
         int *temp = new int[size];
         for(int i = 0; i != (size-1); ++i)
         {
              temp[i] = this->data[i];
         }
         delete [] this->data;
         this->data = temp;
         this->data[size-1]=val;
    }
}

push_back 函数中分配一个具有新大小的新数组并从现有数组中复制数据。删除现有数组后,我们看到this-&gt;data 无法指向有效位置。将新数组的地址分配给this-&gt;data,我们访问现有数据并且大小增加了+1。最后我们将参数val 分配给数组末尾(size-1)。

【讨论】:

    【解决方案2】:

    有一些问题。

    1. 看起来你不需要 0 大小向量的特殊情况

    2. 你没有分配足够的内存:

    例如,如果 size 为 1,则遇到这种情况,然后 size 变为 2,然后分配 ... 1 的缓冲区。

    else
    {
         ++size;
         int *temp = new int[size - 1];
         for(int i = 0; i != (size - 1); i++)
         {
             temp[i] = this->data[i];
         }
    
    Tip: use ```for (int i = 0; i < size; ++i)```  and ```new int[size]```
    
    1. 你在你的循环之后越界了。如果分配 [size] 个字节,则 (size-1) 是最后一个有效索引。

    2. 您将数据复制到 temp,然后将 temp 复制到另一个分配中。你不需要这样做。只需分配 this->data = temp;整个第二个循环是不必要的,不要在最后删除 temp。

    【讨论】:

      【解决方案3】:

      在您的代码中:

      this->data[size] = val;
      

      你正在分配的数组之外。

      在上一个循环中相同(在其最后一次迭代中):

       for(int i = 0; i != (size - 1); i++)
       {
           this->data[i] = temp[i];
       }
      

      【讨论】:

      • 谢谢!接受这个建议,我让它正常工作。
      猜你喜欢
      • 1970-01-01
      • 2019-02-07
      • 2018-05-12
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-18
      • 1970-01-01
      相关资源
      最近更新 更多