附加动态数组并在完成后将其大小加倍
Appending a dynamic array and doubling its size upon completion
创建一个初始空格为 4 的 int 动态数组。编写一个函数"append",将给定值附加到此数组。在任何阶段,如果此函数发现数组已满,它会自动将数组的大小加倍以容纳此新值。还要编写一个函数来显示此数组的所有元素。编写一个 main 来测试所有这些函数。
我尝试解决上述问题,如下所示。但是,我无法获得正确的代码。请帮助我
#include<iostream>
using namespace std;
void append(int*& array, int val, int n, int r)
{
int i,p;
int *array1;
for (i=r;i<n;i++)
array[i] = val;
if(i==n)
{
p = 2*n;
array1 = new int [p];
}
for(int j =0; j<r/2; j++)
array1[j]= array[j];
append(array1, val, p, p/2);
}
int main()
{
int q,p=0,val, n = 4;
int n1 = p/2;
int *array = new int[n];
while(1)
{
cout<<"Enter 0 to end and 1 to continue";
cin>>q;
while(q!=0)
{
cin>>val;
append(array,val,n,n1);
}
}
return 0;
}
我需要在不使用"类"的情况下解决这个问题。我该怎么做?
您的函数需要执行以下操作: 1( 能够检查当前追加调用是否会导致越界写入尝试。所以你需要这样的东西(并给变量像这样的解释性名称(作为函数的第一行:
if (array_size < item_count) {
//double size of array
}
要将数组的大小加倍,您必须创建一个大小为两倍的新数组,从旧数组中复制所有项目,删除旧数组,清空旧数组的指针,并以某种方式更新array_size变量(返回到 main 是一个选项,函数本身中的静态 int 计数器是另一个选项(。您可能还必须将指向新数组的指针返回到 main。或者,也许您可以在使用该指针删除旧数组后将旧指针重新寻址到新数组。这都是为了避免内存泄漏。因此,尝试提出一个方法声明,例如:
int append(int* arrPtr, int value, int array_size, int item_count)
这种特殊的方法意味着 main 在每次追加后都会将数组大小作为 int 发送回。所以你需要一些主要的东西,比如:
array_size = append(array, val, array_size, item_count);
棘手的部分是当您创建新数组时:
array_size = 2 * array_size;
int* temp = new int[array_size]
//copy everything over from old array to new using arrPtr and temp
for (int i = 0; i < array_size / 2; i++) {
temp[i] = arrPtr[i]
}
//delete the contents of the old array:
delete[] arrPtr;
//pointer gymnastics to redirect arrPtr to the new array:
arrPtr = temp;
temp = nullptr;
//okay, now you are back out of the conditional and can use item_count to insert the
//value into the array (doubled or not)
arrPtr[item_count] = value;
//don't forget to return array_size, and remember main must track item_count as well.
return array_size;
这是它的一般要点。这不是一个完整的答案,但应该给你足够的工作。基本上,你的大部分代码都必须重写,以上并不是一个完整的解决方案。祝你好运。
从动态数组的双倍大小中得到提示后,我已经解决了它。
#include<iostream>
using namespace std;
void add_element(int* &array, int &size)
{int count = 0;
while(1)
{
int number;
cout << "What number do you want to add? " << endl;
cin >> number;
if (count == size)
{
int newSize = size * 2;
int *newArr = new int[newSize];
for (int i = 0; i < count; ++i)
{
newArr[i] = array[i];
}
delete[] array;
array = newArr;
size = newSize;
}
array[count] = number;
++count;
int k;
cout<<"Do u want to end, then press 0";
cin>>k;
if(k==0) break;
}
for(int g = 0; g<count; g++)
cout<<array[g]<<'t';
}
int main()
{
int i,j,k,size;
cin>>size;
int* array = new int [size];
add_element(array, size);
}
相关文章:
- std::向量与传递值的动态数组
- 将数组动态分配到具有指针参数的函数中
- 我可以以某种方式使我的矢量/数组动态更改装置数量吗?
- 使用 new: "potentially uninitialized pointer"将对象数组动态分配给指针
- 为什么C 没有方便的方式来为多维数组动态分配内存
- 如何制作具有动态大小的数组?动态数组的一般用法(可能还有指针)
- c++中的数组动态内存分配
- 多维数组动态分配的内存图
- 如何在 c++ 中使用 2-D 固定数组为 4-D 数组动态分配内存
- C++ 我在数组/动态内存方面遇到了一些问题
- C++ 中从数组动态强制转换
- C++中高维数组动态内存分配的正确方法.
- 从 C++ 结构中包含的指针数组动态分配和访问内存
- 对象数组动态内存分配
- 为二维数组动态分配内存时出错
- 如何将2d数组动态分配给结构
- 为结构数组c++动态分配内存
- 将数组动态添加到多维数组
- 类中的类数组-动态数组(c++)有问题
- 分配对象数组动态冻结