在类中创建动态数组(c++)
Create a dynamic array within a class (C++)
我想实现一个类与数组。如果超出了数组的大小,我会调用resize()函数。然而,我似乎不能编码一个动态数组作为数据成员。有人能指导我如何解决这个问题吗?
这是我目前得到的:
class ArrayList
{
public:
ArrayList()
{
array[ARR_SIZE] = {0};
space = 0;
size = ARR_SIZE;
}
void insert(int place, int value)
{
if (place >= size)
cout << "Sorry, that index is not accessible" << endl;
else
{
array[place] = value;
space++;
if(space == size)
allocate();
}
}
void remove(int place)
{
if(place >= size)
cout << "Sorry, that index is not accessible" << endl;
else
{
array[place] = NULL;
space--;
}
}
void allocate()
{
int* array = new int[size*2];
size = size*2;
}
int usedSize()
{
return space;
}
int totalSize()
{
return size;
}
private:
int array[ARR_SIZE];
int space;
int size;
};
使用std::vector
。c++不支持可变长度或动态大小的数组,无论是在类中还是在其他地方。
使用std::vector
并让它为您管理一切:
#include <vector>
class ArrayList
{
public:
ArrayList()
{
array.reserve(ARR_SIZE);
}
void insert(int place, int value)
{
if ((place < 0) || (place > array.size()))
std::cout << "Sorry, that index is not accessible" << std::endl;
else
array.insert(array.begin()+place, value);
}
void remove(int place)
{
if ((place < 0) || (place >= array.size()))
std::cout << "Sorry, that index is not accessible" << std::endl;
else
array.erase(array.begin()+place);
}
int usedSize()
{
return array.size();
}
int totalSize()
{
return array.capacity();
}
private:
std::vector<int> array;
};
如果你真的想自己管理数组内存,那你就大错特错了。试试这个:
class ArrayList
{
public:
ArrayList()
{
array = new int[ARR_SIZE];
space = 0;
size = ARR_SIZE;
}
ArrayList(const ArrayList &src)
{
array = new int[src.size];
space = src.space;
size = src.size;
for(int i = 0; i < space; ++i)
array[i] = src.array[i];
}
~ArrayList()
{
delete[] array;
}
void insert(int place, int value)
{
if ((place < 0) || (place > space))
std::cout << "Sorry, that index is not accessible" << std::endl;
else
{
if (space == size)
allocate();
for(int i = space-1; i > place; --i)
array[i] = array[i-1];
array[place] = value;
++space;
}
}
void remove(int place)
{
if ((place < 0) || (place >= space))
std::cout << "Sorry, that index is not accessible" << std::endl;
else
{
for(int i = place+1; i < space; ++i)
array[i-1] = array[i];
--space;
}
}
void allocate()
{
int* newarray = new int[size*2];
for (int i = 0; i < space; ++i)
newarray[i] = array[i];
delete[] array;
array = newarray;
size *= 2;
}
int usedSize()
{
return space;
}
int totalSize()
{
return size;
}
ArrayList& operator=(const ArrayList &src)
{
int *newarray = new int[src.size];
for(int i = 0; i < src.space; ++i)
newarray[i] = src.array[i];
delete[] array;
array = newarray;
space = src.space;
size = src.size;
return *this;
}
private:
int *array;
int space;
int size;
};
我还没有声誉给你留下评论,但是你试过c++的std::vector
吗?它做的正是你要找的:
听起来应该使用malloc和realloc。这应该和Remy使用new和delete的答案一样。
将缓冲区声明为int*类型,并通过递增的变量跟踪缓冲区的大小。如果您的大小超过动态数组中的空间量,请重新分配。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '