gpt4 book ai didi

c++ - 扩展/收缩动态数组

转载 作者:行者123 更新时间:2023-11-30 04:08:26 26 4
gpt4 key购买 nike

我有一个任务要创建一个模板容器类,其中包含一个可以增加和减少的动态数组。显示数组时,对于数组中展开的元素,我要么得到垃圾数字,要么根本没有数字(因此,如果它在第五个元素上展开,在我输入第六个元素后,该值将变为垃圾)。当我尝试删除元素时,出现调试断言失败错误。

这是我的代码:

template <class T> class container {
public:
container(); // constructor
// Post-condition: count is set to -1 and dynamic array size set to 5
~container(); //destructor
void insert(T &n);
// Pre-condition: a value is passed to the function
// Post-condition: if data array is not full, increment count by 1 and insert the value in the array
// Otherwise, display "Container full! No insertion is made."
void remove();
//Post-condition: if data array is not empty, remove the data[count] element in data array and decrement count by 1;
//otherwise, display a message "Container empty! Nothing is removed from it."
void display();
// Post-condition: if the array is not empty, displays all values in data array similar to the sample output;
//Otherwise, display the message “Container is now empty!"
void fillarray(container c);
//pre-condition: a container c is passed to the function
//post-condition: dynamic array of chosen type is created and filled continuously with user entered values
private:
bool empty;
//Post-condition: returns true is the array is empty, otherwise returns false
T *data; // dynamically allocated array used to store or contain the inserted values
int count; // indicates how many values have been inserted
int max;
};
template <class T> container<T>::container()
{
count = -1;
max = 5;
data = new T[max];
assert(data != NULL);
}
template <class T> container<T>::~container()
{
delete [] data;
}
template <class T> void container<T>::insert(T &n)
{
if (count >= (max - 1))
{
max = max * 2;
cout << "\nContainer full! Array size is increased by " << max/2 << ".";
T *temp = new T[max];
assert(temp != NULL);
for (int i = 0; i < count; i++)
temp[i] = data[i];
delete [] data;
data = temp;
count++;
data[count] = n;
}
else
count++;
data[count] = n;
}
template <class T> void container<T>::remove()
{
empty = count < 0;
if (empty == 1)
{
cout << "\nContainer empty! Nothing is removed from it.";}
else
{
count--;
T *temp1 = new T[max];
assert(temp1 != NULL);
for (int i = 0; i < count; i++)
temp1[i] = data[i];
delete [] data;
data = temp1;
}
}
template <class T> void container<T>::display()
{
empty = count < 0;
if (empty == 1)
{
cout << "\nContainer is now empty!";}
else
{
for (int i = 0; i <= count; ++i)
cout << " " << data[i];
}
}
template <class T> void container<T>::fillarray(container c)
{
char ans;
do
{
T value;
cout << "\nEnter a value:";
cin >> value;
c.insert(value);
cout << "\nAfter inserting, the \"container\" contains:" << endl;
c.display();
cout << "\nEnter more? (Y/y or N/n)";
cin >> ans;
} while (ans == 'Y' || ans == 'y');
for (int i = 0; i <= count; i++)
{
c.remove();
cout << "\nAfter removing a value from it, the \"container\" contains:" << endl;
c.display();
cout << endl;
}
}
// The main driver function to be used to test implementation
int main()
{
char choice;
cout << "\nEnter S for string container, D for double";
cin >> choice;
if (choice == 'S' || choice == 's')
{
container<string> c;
c.display();
c.fillarray(c);
}
else if(choice == 'D' || choice == 'd')
{
container<double> c;
c.display();
c.fillarray(c);
}
return 0;
}

最佳答案

template <class T> void container<T>::fillarray(container c)
{
//...
}

这个函数其实涉及到两个container<T>对象:*thisc .

由于您“按值传递”(函数参数不是引用),cfillarray创建为原始 c 的拷贝在main .在 fillarray你修改c , 删除和更改 c.data ,但是 this->data仍然包含指向原始存储的悬挂指针。不久之后,您会得到未定义的行为;幸运的是,坏事发生了,你可以看出哪里出了问题。

根据 Rule of Three (Plus Two) ,如果一个类有一个析构函数,你可能不应该让编译器生成默认的复制构造函数和复制赋值,你可能要考虑实现一个移动构造函数和移动赋值。

满足该规则的最简单,有时也是最好的方法是禁用复制:

template <class T> class container
{
public:
container(container const&) = delete;
container& operator=(container const&) = delete;
//...
};

然后编译器将确保您不会不小心复制并让自己陷入此类麻烦。

关于c++ - 扩展/收缩动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21842230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com