【问题标题】:How do I compare 2 arrays to see if they have the same contents?如何比较 2 个数组以查看它们是否具有相同的内容?
【发布时间】:2016-04-03 06:16:36
【问题描述】:

到目前为止,我的代码工作正常,但我在第 17 到第 30 行代码中有一个逻辑错误,我似乎无法弄清楚。我觉得我有太多事情要做。

问题提示如下:

如果两个二维数组m1m2 具有相同的内容,则它们是相同的。如果 m1m2 相同,则使用以下标头编写一个返回 true 的函数:

const int SIZE = 3;    

bool equals(const int m1[][SIZE], const int m2[][SIZE]);

编写一个测试程序,提示用户输入两个 3 x3 整数数组,并显示两者是否相同。以下是运行示例(如下)。


Enter m1: 51 25 22 6 1 4 24 54 6

Enter m2: 52 22 25 6 1 4 24 54 6

output : Two arrays are identical

Enter m1: 51 5 22 6 1 4 24 54 6

Enter m2: 51 22 25 6 1 4 24 54 6

output: Two arrays are not identical

这是我的代码(对不起,如果它很糟糕,我还在学习中)

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;

const int SIZE = 3;

bool equals(const int m1[][SIZE], const int m2[][SIZE]) {

    bool choice = true;

    while (choice) {
        for (int i = 0; i < SIZE; i++) {
            for (int j = 0; j < SIZE; j++) {
                if (m1[i][j] != m2[i][j]) {                                                        // code is fine until here. There is a logic error somewhere. m1: 123456789 m2: 987654321 output: array not identical
                    for (int k = 0; k < SIZE; k++) {
                        for (int h = 0; h < SIZE; h++) {
                            if (m1[i][j] == m2[k][h]) {
                                return true;
                            }
                            else {
                                choice = false;
                                return false;
                            }
                        }
                    }
                }
                else {
                    return true;
                }
            }
        }

    }
}

int main() {

    string input1, input2, num1, num2;
    int count1 = 0, count2 = 0;
    int a = 0, b = 0, c = 0;
    int a2 = 0, b2 = 0, c2 = 0;
    int arr1[SIZE][SIZE];
    int arr2[SIZE][SIZE];

    cout << "Enter m1: ";
    getline(cin, input1);
    stringstream line1(input1);

    cout << "Enter m2: ";
    getline(cin, input2);
    stringstream line2(input2);

    while (line1 >> num1) {

        if (count1 < 3) {
            stringstream(num1) >> arr1[0][a];
            a++;
        }
        else if (count1 >= 3 && count1 <= 5) {
            stringstream(num1) >> arr1[1][b];
            b++;
        }
        else {
            stringstream(num1) >> arr1[2][c];
            c++;
        }
        count1++;
    }

    while (line2 >> num2) {

        if (count2 < 3) {
            stringstream(num2) >> arr2[0][a2];
            a2++;
        }
        else if (count2 >= 3 && count2 <= 5) {
            stringstream(num2) >> arr2[1][b2];
            b2++;
        }
        else {
            stringstream(num2) >> arr2[2][c2];
            c2++;
        }
        count2++;
    }

    bool answer = equals(arr1, arr2);

   if (answer) {
        cout << "Two arrays are identical" << endl;
   }
   else {
        cout << "Two arrays are not identical" << endl;
   }

   system("Pause");

   return 0;
}

谢谢

【问题讨论】:

  • 肯定只需要一个的差异就可以返回false吗?
  • 你为什么使用while?为什么你有这么多嵌套的fors?如果要比较二维数组,请将其视为具有行和列的表。您必须遍历行并在每一行中遍历列。这意味着,只需要 2 个嵌套的 fors
  • 既然你有一个简单数据类型的静态分配和连续数组,考虑使用memcmpen.cppreference.com/w/cpp/string/byte/memcmp

标签: c++ c++11 multidimensional-array


【解决方案1】:

您在该功能中确实有很多事情要做。你有很多嵌套循环。

要使代码更易于理解,您可以做的就是仅使用两个 for 循环(嵌套)检查错误条件。如果遇到它,返回false。如果没有遇到 false 条件,您将在某个时刻离开循环,然后确保遇到 return true 语句。

for(int i = 0; i < SIZE; ++i)
    for(int j = 0; j <SIZE; ++j)
        if (m1[i][j] != m2[i][j])
            return false;

return true;

这就是我编写该函数的方式。 这些是您在函数中的一些冗余:

  • while 循环。那有什么用?您已经有一个 for 循环,它将一直运行到满足结束条件。

  • 第一个 if 条件中的另一组 for 循环。

即使不满足所需条件,您的函数也会返回 true。即使 a1[1][2] 等于 a2[3][3],您的第 21 行也会返回 true。不应该是这样的。

找出这些错误的一种方法是在一张纸上编写测试用例。

希望我能帮上忙! (我希望我是对的):)

【讨论】:

  • 为了可读性和新的编码人员,我可以建议使用大括号来使范围明确和明显吗?
最近更新 更多