创建自定义矢量 class。 Push_back 函数仅适用于第一个值
Creating a custom vector class. Push_back function only working for the first value
在我的 Comp Sci class 中,我们正在学习如何制作我们自己的向量 class。我们最终会将定制的字符串 class 对象存储在定制的向量 class 中。为了简单起见,我想事先尝试构建一个整数向量 class。
到目前为止,我有一个默认构造函数,它将我的指针初始化为一个空数组并将大小设置为 0。然后我尝试使用我的 push_back 函数附加一些值,然后检查以确保它正确完成。
当我做 std::cout << v[0] << std::endl;
我得到了正确的输出 (10)。但是,如果我再次调用 push_back 然后调用 v[1] 我得到 0.
我觉得我在 push_back 函数中没有正确分配内存,但我不确定。
感谢任何建议!
[第 1 部分][1]
[第 2 部分][2]
抱歉,如果我的格式有误,我是新手。
class:
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;
}
}
在您的代码中:
this->data[size] = val;
您将超出分配的数组范围。
与上一个循环(在最后一次迭代中)相同:
for(int i = 0; i != (size - 1); i++)
{
this->data[i] = temp[i];
}
有一些问题。
看起来你不需要 0 大小向量的特例
您没有分配足够的内存:
例如,如果 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]```
你在循环后出界了。如果分配 [size] 个字节,则 (size-1) 是最后一个有效索引。
您将数据复制到临时文件中,然后将临时文件复制到另一个分配中。你不需要那样做。只需分配 this->data = temp;整个第二个循环是不必要的,最后不要删除temp。
不需要很多new
和delete
操作和循环。我修复并清理了你的两个功能。
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->data
无法指向有效位置。将新数组的地址分配给 this->data
并且我们访问现有数据并且大小增加 +1。最后我们将参数 val
分配给数组末尾 (size-1).
在我的 Comp Sci class 中,我们正在学习如何制作我们自己的向量 class。我们最终会将定制的字符串 class 对象存储在定制的向量 class 中。为了简单起见,我想事先尝试构建一个整数向量 class。
到目前为止,我有一个默认构造函数,它将我的指针初始化为一个空数组并将大小设置为 0。然后我尝试使用我的 push_back 函数附加一些值,然后检查以确保它正确完成。
当我做 std::cout << v[0] << std::endl;
我得到了正确的输出 (10)。但是,如果我再次调用 push_back 然后调用 v[1] 我得到 0.
我觉得我在 push_back 函数中没有正确分配内存,但我不确定。
感谢任何建议!
[第 1 部分][1]
[第 2 部分][2]
抱歉,如果我的格式有误,我是新手。
class:
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;
}
}
在您的代码中:
this->data[size] = val;
您将超出分配的数组范围。
与上一个循环(在最后一次迭代中)相同:
for(int i = 0; i != (size - 1); i++)
{
this->data[i] = temp[i];
}
有一些问题。
看起来你不需要 0 大小向量的特例
您没有分配足够的内存:
例如,如果 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]```
你在循环后出界了。如果分配 [size] 个字节,则 (size-1) 是最后一个有效索引。
您将数据复制到临时文件中,然后将临时文件复制到另一个分配中。你不需要那样做。只需分配 this->data = temp;整个第二个循环是不必要的,最后不要删除temp。
不需要很多new
和delete
操作和循环。我修复并清理了你的两个功能。
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->data
无法指向有效位置。将新数组的地址分配给 this->data
并且我们访问现有数据并且大小增加 +1。最后我们将参数 val
分配给数组末尾 (size-1).