简体   繁体   English

通过在堆上创建一个新数组在第三个上合并两个排序数组

[英]Merging two sorted array on third by creating a new array on heap

I have a class array inside which I have declared an array its size and length.我有一个 class 数组,我在其中声明了一个数组的大小和长度。 I am trying to merge two sorted arrays by creating the third array on the heap and both the sorted array will be merged on the third array.我试图通过在堆上创建第三个数组来合并两个排序的 arrays,并且两个排序的数组都将合并到第三个数组上。 But whenever I create a new arr on heap the compiler gives me this error: request for member '..' in '..' which is of non-class type但是每当我在堆上创建一个新的 arr 时,编译器都会给我这个错误:request for member '..' in '..' which is of non-class type

class Array
{
public:

    int A[10];
    int length;
    int Size;

};
void display(Array arr)
{
    int i;
    for(i=0;i<arr.length;i++)
    {
        cout<<arr.A[i]<<" ";
    }
}
void Merge(Array *arr1,Array *arr2)
{
    int i,j,k;
    i=j=k=0;
   int *arr3;
   arr3=new int[10];
    while(i<arr1->length && j<arr2->length)
    {
        if(arr1->A[i]<arr2->A[j])
            arr3->A[k++]=arr1->A[i++];
        else
          arr3->A[k++]=arr2->A[j++];
    }
    for(;i<arr1->length;i++)
    {
         arr3->A[k++]=arr1->A[i];
    }
     for(;j<arr2->length;j++)
    {
         arr3->A[k++]=arr1->A[j];
    }

}
int main()
{

   Array arr1{{1,3,5,7},4,4};
Array arr2{{2,4,6,8},4,4};
Array *arr3;
arr3=Merge(&arr1,&arr2);
display(*arr3);
    return 0;
}

The root cause of all your problems is that you use C-Style array with a magical size 10. Like in int A[10];所有问题的根本原因是您使用了神奇大小为 10 的 C 样式数组。就像int A[10]; . . This is a major problem and should be avoided in C++.这是一个主要问题,应该在 C++ 中避免。

Additionally, and the same, In C++ we usually do not use raw pointer for owned memories or new and such stuff.此外,同样地,在 C++ 中,我们通常不对拥有的内存或new内存等使用原始指针。

Anyway.反正。 The design will never work, if the number of elements in both Array classes is greater then 5. Because then you will definitely get an out of bounds problem.如果两个 Array 类中的元素数都大于 5,则设计将永远无法工作。因为那样你肯定会遇到越界问题。

You must use a std::vector .您必须使用std::vector

So, all bad.所以,一切都很糟糕。 But I know that I will hear now, that the teacher said, no vector but new .但我知道我现在会听到,老师说,no vector but new The teacher should be fired or begin to teach C instead of C++.老师应该被解雇或开始教 C 而不是 C++。

Anyway again, I will fix the major bugs for you.无论如何,我会再次为您修复主要错误。 But the sorting algorithm will work neither.但是排序算法都不起作用。

So,所以,

  • If you want to return an Array, then change the signature of your function aand return an Array.如果你想返回一个数组,然后更改你的 function 的签名并返回一个数组。
  • You do want to have a new Array, not new intes.您确实想要一个新数组,而不是新整数。 So, please allocate a new Array instead.所以,请改为分配一个新数组。
  • Do not forget to release the newed Arrary at then end.不要忘记在最后发布新的 Arrary。
  • Set size and length of the new array.设置新数组的大小和长度。
  • Refactor your complete code.重构您的完整代码。

Code example with some fixes:带有一些修复的代码示例:

#include <iostream>
class Array
{
public:

    int A[10];
    int length;
    int Size;
};
void display(Array arr)
{
    int i;
    for (i = 0; i < arr.length; i++)
    {
        std::cout << arr.A[i] << " ";
    }
}
Array* Merge(Array* arr1, Array* arr2)
{
    int i, j, k;
    i = j = k = 0;
   
    Array *arr3 = new Array;

    while (i < arr1->length && j < arr2->length)
    {
        if (arr1->A[i] < arr2->A[j])
            arr3->A[k++] = arr1->A[i++];
        else
            arr3->A[k++] = arr2->A[j++];
    }
    for (; i < arr1->length; i++)
    {
        arr3->A[k++] = arr1->A[i];
    }
    for (; j < arr2->length; j++)
    {
        arr3->A[k++] = arr1->A[j];
    }
    arr3->length = arr1->length + arr2->length;
    return arr3;
}
int main()
{

    Array arr1{ {1,3,5,7},4,4 };
    Array arr2{ {2,4,6,8},4,4 };
    Array* arr3;
    arr3 = Merge(&arr1, &arr2);
    display(*arr3);
    delete[]arr3;
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM