【问题标题】:Merge of 2 arrays using array based list c++使用基于数组的列表 C++ 合并 2 个数组
【发布时间】: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


【解决方案1】:

在ABlist operator + (ABlist a1) 中,您通过值传递a1,这意味着创建了一个临时副本。这将复制任何指针,但不会复制它们指向的值。所以在函数结束时,这个临时副本被销毁,~ABlist 被调用,list_array 被删除。现在原始对象有一个悬空指针,迟早会导致崩溃。

解决办法是通过const引用1:

ABlist operator + (const ABlist &a1) { ... }

可能导致崩溃的第二个问题是,由于某种原因,您的临时缓冲区的容量仅为 100。那么如果你合并两个向量,每个向量有 100 个元素,会发生什么?计算出所需的大小应该很容易。

第三个是复制数组的for循环中的逻辑错误。循环条件是i &lt; a1.actual_size,因此,if 条件i &gt; a1.actual_size 在此循环中永远不会为真。

我不确定您到底想要实现什么,但一个简单的解决方案是遍历当前对象 (this) 的所有项目并在每个项目上使用 insertItem,然后迭代对a1的所有项目


1 并且可能编写一个复制构造函数

【讨论】:

  • 好收获!这同样适用于返回值。所以写拷贝构造函数是必须的。
猜你喜欢
  • 2021-12-05
  • 2017-06-02
  • 2021-07-27
  • 1970-01-01
  • 2015-01-09
  • 1970-01-01
  • 2014-08-04
  • 1970-01-01
  • 2021-06-10
相关资源
最近更新 更多