如何比较两个数组的相似性 在比较C++中的两个数组时,我一直在努力调整这个代码。我在这个网站上看到了其他的例子,并根据建议相应地调整了我的代码。但是当我使用相同的元素和不同的元素以及不同大小的数组运行这段代码时。它总是说它是真的,数组是相等的。有人对如何进行适当的纠正有什么建议吗?谢谢 #include<iostream> using namespace std; int main(){ int arr1[6] ={1,2,3,4,5,6}; int arr2[6] ={1,2,3,4,5,6}; int *p1 = arr1; int *p2 = arr2; bool equal = true; int index = 0; while(index < 6){ if(p1[index] == p2[index]){ equal = true; index ++; } else { equal = false; break; } } if(equal == true) cout<<" The arrays are equal"<< endl; else cout<<" The arrays are equal"<< endl; return 0; } #包括 使用名称空间std; int main(){ int arr1[6]={1,2,3,4,5,6}; int arr2[6]={1,2,3,4,5,6}; int*p1=arr1; int*p2=arr2; 布尔等于真; int指数=0; 而(指数

如何比较两个数组的相似性 在比较C++中的两个数组时,我一直在努力调整这个代码。我在这个网站上看到了其他的例子,并根据建议相应地调整了我的代码。但是当我使用相同的元素和不同的元素以及不同大小的数组运行这段代码时。它总是说它是真的,数组是相等的。有人对如何进行适当的纠正有什么建议吗?谢谢 #include<iostream> using namespace std; int main(){ int arr1[6] ={1,2,3,4,5,6}; int arr2[6] ={1,2,3,4,5,6}; int *p1 = arr1; int *p2 = arr2; bool equal = true; int index = 0; while(index < 6){ if(p1[index] == p2[index]){ equal = true; index ++; } else { equal = false; break; } } if(equal == true) cout<<" The arrays are equal"<< endl; else cout<<" The arrays are equal"<< endl; return 0; } #包括 使用名称空间std; int main(){ int arr1[6]={1,2,3,4,5,6}; int arr2[6]={1,2,3,4,5,6}; int*p1=arr1; int*p2=arr2; 布尔等于真; int指数=0; 而(指数,c++,arrays,comparison,C++,Arrays,Comparison,如果符合以下条件,请修改最后一个: if(equal == true) cout<<" The arrays are equal"<< endl; else cout<<" The arrays are not equal"<< endl; if(equal==true) 你能举一个例子吗,它计算的是真,但你期望的是假?你正在打印“数组相等”在if-else语句的两个分支上。请查看并考虑使用std::equal来

如果符合以下条件,请修改最后一个:

 if(equal == true)

     cout<<" The arrays are equal"<< endl;

  else 

     cout<<" The arrays are not equal"<< endl;
if(equal==true)

你能举一个例子吗,它计算的是真,但你期望的是假?你正在打印
“数组相等”
if
-
else
语句的两个分支上。请查看并考虑使用
std::equal
来代替。不幸的是,OP编辑了问题以解决问题。但我还是+1。抱歉,我的一位朋友指出了我的错误。我更正了错误。谢谢。我还有一个问题要问我马上就起床。