初始化扩展数组元素
所以我已经能够学习扩展数组,但想知道如何将元素添加到扩展的空间中,这是我到目前为止所拥有的。
int *expand(int *arr, int size)
{
int *newArray;
newArray = new int[size * 2];
memcpy( newArray, arr, size * sizeof(int));
for (int index = 5; index < size; index++)
newArray[index] = 0;
return newArray;
}
So i've been able to learn to expand an array, but was wondering how i would go about adding elements to the expanded space, here is what i have so far.
int *expand(int *arr, int size)
{
int *newArray;
newArray = new int[size * 2];
memcpy( newArray, arr, size * sizeof(int));
for (int index = 5; index < size; index++)
newArray[index] = 0;
return newArray;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的代码中可能存在错误。 newArray 的大小为“size * 2”,但您只能初始化“size”之前的元素。
试试这个:
这应该将新空间中的所有元素初始化为 0。
I think you might have a bug in your code. newArray is of size "size * 2", but you initialize elements only up until "size".
Try this:
This should initialize all the elements in the new space to 0's.