char a[1][2];
char b[1][2];
a[0][0] = 'a';
a[0][1] = 'b';
b[0][0] = 'a';
b[0][1] = 'b';
if(a[0] == b[0]){
cout << "worked\n";
}
So as far as I can tell, this comparison between arrays of characters doesn't work the way you would expect it to. The if statement does not execute because the condition a == b
returns false. Why is this?
所以据我所知,字符数组之间的这种比较并不像你期望的那样工作。 if语句不会执行,因为条件a == b返回false。为什么是这样?
2 个解决方案
#1
5
In this statement
在这个声明中
if(a == b){
cout << "worked\n";
}
a and b are implicitly converted to pointers to first elements of the corresponding arrays. So there is a comparison of two pointers. As the arrays occupy different areas in memory then the condition will be alwasy equal to false.
a和b被隐式转换为指向相应数组的第一个元素的指针。所以有两个指针的比较。由于数组占用内存中的不同区域,因此条件将等于假。
There is no such an operation as the comparison for arrays. To compare two arrays you have toc compare all elements of the arrays with each other. For example you could use standard algorithm std::equal. For example
没有像数组比较这样的操作。要比较两个数组,您需要将数组的所有元素相互比较。例如,您可以使用标准算法std :: equal。例如
if( std::equal( std::begin( a ), std::end( a ), std::begin( b ) ) ){
cout << "worked\n";
}
Another approach is to use standard container std::array that has the comparison operator. For example
另一种方法是使用具有比较运算符的标准容器std :: array。例如
std::array<char, 2> a = { 'a', 'b' };
std::array<char, 2> b = { 'a', 'b' };
if(a == b){
cout << "worked\n";
}
#2
4
You can not compare arrays like that. You need to iterate over the arrays and compare each pair of elements in turn. Alternatively(and preferably) replace the static array with std::vector
. The code you show compares the pointers a
and b
which of course are not equal.
你无法比较那样的数组。您需要迭代数组并依次比较每对元素。或者(并且优选地)用std :: vector替换静态数组。您显示的代码比较指针a和b当然不相等。
#1
5
In this statement
在这个声明中
if(a == b){
cout << "worked\n";
}
a and b are implicitly converted to pointers to first elements of the corresponding arrays. So there is a comparison of two pointers. As the arrays occupy different areas in memory then the condition will be alwasy equal to false.
a和b被隐式转换为指向相应数组的第一个元素的指针。所以有两个指针的比较。由于数组占用内存中的不同区域,因此条件将等于假。
There is no such an operation as the comparison for arrays. To compare two arrays you have toc compare all elements of the arrays with each other. For example you could use standard algorithm std::equal. For example
没有像数组比较这样的操作。要比较两个数组,您需要将数组的所有元素相互比较。例如,您可以使用标准算法std :: equal。例如
if( std::equal( std::begin( a ), std::end( a ), std::begin( b ) ) ){
cout << "worked\n";
}
Another approach is to use standard container std::array that has the comparison operator. For example
另一种方法是使用具有比较运算符的标准容器std :: array。例如
std::array<char, 2> a = { 'a', 'b' };
std::array<char, 2> b = { 'a', 'b' };
if(a == b){
cout << "worked\n";
}
#2
4
You can not compare arrays like that. You need to iterate over the arrays and compare each pair of elements in turn. Alternatively(and preferably) replace the static array with std::vector
. The code you show compares the pointers a
and b
which of course are not equal.
你无法比较那样的数组。您需要迭代数组并依次比较每对元素。或者(并且优选地)用std :: vector替换静态数组。您显示的代码比较指针a和b当然不相等。