【发布时间】:2019-12-07 13:34:45
【问题描述】:
我有一个合并 2 个数组的任务,通过在类中使用 + 运算符。我想在第一个列表的末尾复制第二个列表。但问题是在合并列表时代码会崩溃(a3=a1+a2)。
在main中我创建了3个对象,前2个对象将由字符串类型的用户填充,然后将使用运算符重载将上述2个列表合并,但是当它到达时它会崩溃.
对不起我的英语 谢谢
#include<iostream>
#include<string>
using namespace std;
template<class T>
class ABlist
{
private:
int max_size;
int actual_size;
T *list_array;
public:
ABlist(int size)
{
max_size = size;
list_array = new T[max_size];
actual_size = 0;
}
~ABlist()
{
delete[]list_array;
}
bool isEmpty()
{
if (actual_size == 0)
{
return true;
}
else
{
return false;
}
}
bool isFull()
{
if (actual_size == max_size)
{
return true;
}
else
{
return false;
}
}
int getlength()
{
return actual_size;
}
T getitem(int index)
{
return list_array[index];
}
void insertitem(T value)
{
if (!isFull())
{
if (search(value) == -1)
{
list_array[actual_size] = value;
actual_size++;
}
else
{
cout << " item already present in the list" << endl;
}
}
else
{
cout << " List is Full" << endl;
}
}
int search(T value1)
{
int flag = 0;
for (int i = 0; i < actual_size; i++)
{
if (list_array[i] == value1)
{
return i+1;
break;
}
}
return -1;
}
void removeitem(int index1)
{
if (!isEmpty())
{
if (index1 > 0 && index <= actual_size)
{
for (int i = index1; i < actual_size; i++)
{
list_array[i] = list_array[i + 1];
}
actual_size--;
cout << " Item Successfull removed from the list" << Endl;
}
else
{
cout << " Index is not in the range" << endl;
}
}
else
{
cout << " list is empty " << Endl;
}
}
int findandremoveitem(T value1)
{
int save;
if (!isEmpty)
{
//save = search(value1);
if (save != -1)
{
for (int i = save; i < actual_size; i++)
{
list_array[i] = list_array[i + 1];
}
actual_size--;
return save;
}
else
{
return -1;
}
}
}
void displayList()
{
for (int i = 0; i < actual_size; i++)
{
cout << " " << list_array[i] << endl;
}
}
void clearlist()
{
for (int i = 0; i < max_size; i++)
{
list_array[i] = -1;
}
cout << " List is cleared" << endl;
}
void Resizelist()
{
max_size = 2 * size;
int *ptr1, *ptr2;
ptr1 = new int[max_size];
for (int i = 0; i < actual; i++)
{
ptr1[i] = list_array[i];
}
ptr2 = list_array;
delete[]list_array;
list_array = ptr1;
}
ABlist operator + (ABlist a1)
{
ABlist temp(100);
int k = 0;
for (int i = 0; i < a1.actual_size; i++)
{
if (i > a1.actual_size)
{
temp.list_array[i] = list_array[k];
k++;
temp.actual_size++;
}
else
{
temp.list_array[i] = a1.list_array[i];
temp.actual_size++;
}
}
return temp;
}
};
void main(void)
{
int size = 100;
ABlist<string> a1(size);
ABlist<string> a2(size);
ABlist<string>a3(size);
string name;
string name1;
int pos;
cout << " FILL out the first array" << endl;
for (int i = 0; i < 2; i++)
{
cin >> name;
a1.insertitem(name);
}
cout << " Fill out the second array" << endl;
for (int i = 0; i < 2; i++)
{
cin >> name1;
a2.insertitem(name1);
}
cout << " Array 1" << endl;
a1.displayList();
cout << " Array 2" << endl;
a2.displayList();
cout << " After merge" << endl;
a3 = a1 + a2;
a3.displayList();
}
【问题讨论】:
-
这不是真正的minimal reproducible example,因为有很多代码与您的问题无关。
-
您创建大小为 100 的列表。在
operator+中,临时对象的容量也为 100。这可能适用于每个元素的主要示例,但对于较大的大小,这是肯定的崩溃的方式。 -
你应该如何“合并”这两个数组?将一个附加到另一个?在数组之间交替?以其他顺序?
-
嗨 Sharjeel,您的代码有很多错别字。您能否分享一下您编译和使用的确切代码?
-
Kirjain,它与我编译的代码相同,实际上这段代码包含不同的类函数,它们是我的作业的一部分,它们运行良好,但只有运算符重载导致问题一次又一次地崩溃,我不知道为什么。
标签: c++ oop data-structures