如何比较两个数组以查看它们是否具有相同的内容
How do I compare 2 arrays to see if they have the same contents?
到目前为止,我的代码可以工作,但在第17到第30行代码中出现了逻辑错误,我似乎无法解决。我觉得我有太多事情要做。
问题提示如下:
如果两个二维阵列CCD_ 1和CCD_。如果m1
和m2
相同,则使用以下标头编写一个返回true的函数:
const int SIZE = 3;
bool equals(const int m1[][SIZE], const int m2[][SIZE]);
编写一个测试程序,提示用户输入两个3x3的整数数组,并显示两者是否相同。以下是示例运行(如下)。
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;
}
谢谢
这个函数中确实有很多内容。你有很多嵌套的循环。
要使代码更容易理解,只需使用两个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。事实并非如此。
找出这些错误的一种方法是在一张纸上写出测试用例。
我希望我能帮上忙!(我希望我是对的):)
相关文章:
- std::less是否应该允许在编译时比较不相关的指针?
- 对于BTreeMap和其他依赖于Ord的东西,是否有等效于C++比较器对象?
- 用户定义的结构是否有默认C++比较器?
- gcc 是否在 2 条短裤的过度对齐结构的比较中错过了优化机会?
- 这种比较是否不一致(或者存在其他问题)?
- 通过默认复制构造函数比较 C++ 字符串是否会影响性能,原因为何?
- 是否可以在C++中使用三元运算符在 if 语句中选择比较运算符?
- 我如何比较这些不同的输出,看看它们是否相等
- C++中是否有任何常量可以用作比较中的最小值/最大值
- 函数指针的比较是否合法
- 是否有任何统一的方法可以在 c++ 中创建自定义比较器?
- C++是否保证标准容器比较中操作数的顺序?
- C++中的结构是否按顺序分配内存?每次都以某种方式获得指针比较的正确答案
- C 是否具有对两个STD :: sets,vectors等进行三角比较的标准方法
- 比较BGR图像是否完全相同
- qsort C++是否曾经将元素与自身进行比较?
- 是否可以将两个任意函数与 C++17 中的 std::any 进行比较
- 是否可以比较两个函子的相等性
- 将无符号的 int 与 std::string::size_type 进行比较是否安全
- 三向比较运算符是否始终有效