我正在尝试使用类在 C++ 中创建数组数据结构。这个想法是,数组有一个预定义的长度,每当需要额外空间时,数组就会被重新分配,以使其先前长度的两倍。但是,代码有问题,我无法弄清楚。它显示应该分配的项目的垃圾值。
以下是相关代码:
#include <iostream>
using namespace std;
class Array
{
private:
int length = 8;
int occupied_length = 0;
int* array = new int[length];
void reallocate_array()
{
array = (int*) realloc(array, length*2);
length = length * 2;
}
public:
Array()
{
cout << "How many elements do you want in the array: ";
cin >> occupied_length;
if (occupied_length > length) // In case array is full, it needs to be reallocated
reallocate_array();
for (int i = 0; i < occupied_length; i++)
cin >> *(array + i);
Print();
}
~Array() { delete array; }
void insert(int pos, int data)
{
if (pos > occupied_length) {cout << "Invalid Position!\n"; return; }
occupied_length += 1;
if (occupied_length > length) { reallocate_array(); }
if (pos == occupied_length){ *(array+occupied_length) = data; return;}
for(int i = occupied_length-2; i >= pos-1; i--)
*(array+i+1) = *(array+i);
*(array + pos - 1) = data;
}
int size()
{
return occupied_length;
}
void Print()
{
for(int i =0; i <occupied_length; i++) { cout << *(array+i) << " "; }
cout << endl;
}
};
int main()
{
Array arr;
arr.insert(7,45);
arr.Print();
arr.insert(3,35);
arr.Print();
arr.insert(4,86);
arr.Print();
cout << arr.size() << endl;
return 0;
}
最重要的是,程序在最后一个 cout 语句之后不会立即停止执行。它等待一两秒钟。
非常感谢您的帮助。谢谢