创建自定义矢量类.Push_back函数仅适用于第一个值

Creating a custom vector class. Push_back function only working for the first value

本文关键字:函数 适用于 第一个 back 自定义 Push 创建      更新时间:2024-09-21

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

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

当我做std::cout<lt;v[0]<lt;std::endl;

我得到了正确的输出(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();
};

main:

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

在您的代码中:

this->data[size] = val;

您正在超出已分配的数组。

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

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

有一些问题。

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

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

例如,如果大小为1,则遇到这种情况,则大小变为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]```
  • 循环后,您将出界。如果分配[size]字节,则(size-1(是最后一个有效索引。

  • 将数据复制到temp中,然后将temp复制到另一个分配中。你不需要那样做。只需分配此->data=温度;整个第二个循环是不必要的,并且不要在最后删除temp。

  • 对于许多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;
    }
    }
    

    在CCD_ 3函数中,分配具有新大小的新数组并从现有数组中复制数据。删除现有数组后,我们发现this->data无法指向有效位置。将新数组的地址分配给this->data,我们访问现有数据,大小增加+1。最后,我们将参数val分配给数组的末尾(size-1(。