c++ 数组排序函数的输出不正确

col17t5w  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(158)

每当我在Visual Studio中运行此程序时,我都会遇到以下问题。

#include <iostream>

using namespace std;

int* append(int*, int, int*, int);
int* merge(int*, int, int*, int);
void print(int*, int, const char*);

int main()
{
    int arrayA[] = { 11,33,55,77,99 };  // use other values for more tests
    int arrayB[] = { 22,44,66,88 };
    print(arrayA, 5, "Sorted array A: ");
    print(arrayB, 4, "Sorted array B: ");
    int* arrayC = append(arrayA, 5, arrayB, 4); // arrayC points to the appended array
    print(arrayC, 9, "Append B to A:  ");
    int* arrayD = merge(arrayA, 5, arrayB, 4);
    print(arrayD, 9, "Merge A to B:   ");
    delete[] arrayC;
    delete[] arrayD;

    return 0;

}

int* append(int* arrayA, int sizeA, int* arrayB, int sizeB)
{
    int num = sizeA + sizeB;
    int* appendArray = new int[num];

    for (int i = 0; i < sizeA; i++)
    {
        appendArray[i] = arrayA[i];
    }

    for (int i = sizeA - 1, j = 0; i < (sizeB + sizeA); i++, j++)
    {
        appendArray[i] = arrayB[j];
    }

    return appendArray;
}

int* merge(int* arrayA, int sizeA, int* arrayB, int sizeB)
{
        int num = sizeA + sizeB;
        int* mergeArray = new int[num];
        int i = 0, j = 0, k = 0;

    do
    {
        if (arrayA[i] <= arrayB[j])
        {
            mergeArray[k] = arrayA[i];

            i++;
            k++;
        }
        else
        {
            mergeArray[k] = arrayB[i];

            j++;
            k++;
        }
    } while (k < sizeof(mergeArray));

    return mergeArray;
}

void print(int* array, int size, const char* lable)
{
    cout << lable;

    for (int i = 0; i < size; i++)
    {
        cout << array[i] << " ";
    }

    cout << endl;
}

此程序当前输出:

Sorted array A: 11 33 55 77 99
Sorted array B: 22 44 66 88
Append B to A:  11 33 55 77 22 44 66 88 -858993460
Merge A to B:   11 44 33 66 55 88 77 -858993460 -842150451

我也不知道为什么。
预期输出为:

Sorted array A: 11 33 55 77 99
Sorted array B: 22 44 66 88
Append B to A:  11 33 55 77 99 22 44 66 88
Merge A to B:   11 22 33 44 55 66 77 88 99
s4n0splo

s4n0splo1#

这不一定是对您的直接回答,但更一般地是关于为什么不在当前的C++中使用“C”样式数组的回答。
不推荐使用这样的“C”样式数组,因为客户端代码不知道三件事。

One:merge/append的客户端代码将不知道返回的指针指向数组还是int,该信息将丢失。
Two:如果返回的指针是指向数组的指针,则大小丢失。
:客户端将不知道他必须在返回的指针上调用delete。所有权信息丢失。

那么在当前的C中(naked)new/delete的使用应该被最小化。当你可以的时候尝试使用vector这样的容器。如果你仍然需要分配内存,使用std::make_unique/std::unique_ptr
为了给予您一个概念,这是使用当前C
及其标准库的类似代码。

#include <algorithm>  // for sort
#include <iostream>
#include <vector>

//using namespace std; // NO Don't ever do this.

// Instead of making a print function, overload stream operator <<
// instead of print, advantage you will have code that can write to files 
// and other streams too

std::ostream& operator<<(std::ostream& os, const std::vector<int>& values)
{
    os << "[";
    bool comma = false;

    for (const int value : values)
    {
        if (comma) os << ", ";
        os << value;
        comma = true;
    }

    os << "]";

    return os;
}

// append can now return an array as vector (which also knows its size) Don't worry about copy of the array on return C++ has copy elision (1)
std::vector<int> append(const std::vector<int>& values1, const std::vector<int>& values2)
{
    std::vector<int> retval{ values1 }; // make a copy of values1
    retval.insert(retval.end(), values2.begin(), values2.end()); // add second array
    return retval;
}

std::vector<int> merge(const std::vector<int>& values1, const std::vector<int>& values2)
{
    auto retval = append(values1, values2);
    std::sort(retval.begin(), retval.end());
    return retval;
}

int main()
{
    std::vector<int> arrayA{ 11,99,55,77,33 };  // use other values for more tests
    std::vector<int> arrayB { 88,44,22,66 };

    std::sort(arrayA.begin(), arrayA.end());
    std::sort(arrayB.begin(), arrayB.end());

    std::cout << "Sorted array A " << arrayA << "\n"; // no need to use endl (that flushes)
    std::cout << "Sorted array B " << arrayB << "\n"; // no need to use endl (that flushes)

    std::cout << "A appended with B " << append(arrayA, arrayB) << "\n";
    std::cout << "A merged with B " << merge(arrayA, arrayB) << "\n";

    //delete[] arrayC; No need vectors will delete themselves. RAII  (2) behavior

    return 0;

}

(1)(2)第一个

相关问题