创建自定义矢量类.Push_back函数仅适用于第一个值
Creating a custom vector class. Push_back function only working for the first value
在我的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];
}
有一些问题。
-
看起来不需要0大小矢量的特殊情况
-
您没有分配足够的内存:
例如,如果大小为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。
对于许多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;
}
}
在CCD_ 3函数中,分配具有新大小的新数组并从现有数组中复制数据。删除现有数组后,我们发现this->data
无法指向有效位置。将新数组的地址分配给this->data
,我们访问现有数据,大小增加+1。最后,我们将参数val
分配给数组的末尾(size-1(。
- 使用一个参数的模板函数时出错(适用于 2)
- 模板函数仅适用于VS
- 如何使此函数适用于 100 个对象 (c++)?
- 没有适用于 std::unique_ptr 的适当默认构造函数
- 继承函数是否适用于 C++ 中的基类元素或派生类元素?
- 为什么不区分大小写适用于 std::unordered_set的 std::hash 函数?
- 如何创建适用于 lambda 表达式的排序函数
- Qt C++macOS问题。我正在使用函数 .find( "a word") 在多集中搜索单词,它适用于 Windows 但不适用于 mac
- 多态性是否适用于值?或者在按(基)值返回时使用派生类的移动构造函数
- 函数指针数组的类模板参数推导适用于 clang,但不适用于 gcc
- 为什么模板函数只基于返回类型适用于C++
- 是否有适用于 stream2 或 stream3 函数的代码
- 为什么OpenCV构造函数适用于iPhone5而不适用于iPhone6/iPad
- 声明引用适用于类,但不适用于主函数
- 为什么函数模板不理解 NULL,但适用于 nullptr
- 如何获取 Linux 中 mmap() 函数的 FileDescriptor 的内存地址,适用于 Video4Linux
- 在构造函数类中初始化 ofstream,仅适用于 c++11
- C++模板机制来获取函数参数的数量,这将适用于 lambda 和普通函数
- 名称篡改是否适用于c++中的虚拟函数
- 如何在新程序中包含来自其他c++文件的函数(适用于Mac OSX应用程序)