为什么 c++ 中 x[y] == y[x]?
有人告诉我这个......我一开始并不相信他们,但它确实有效。如果 x 和 y 在整个代码中没有改变,为什么会这样:
int x [5] = { 0,1,2,3,4};
int y = 3;
if(x[y] == y[x]){
cout << "Why..." << endl;
}
索引 y 中 x 数组的值如何 = 数组 y 中 x 索引的值?但没有 y 数组。
Possible Duplicate:
In C arrays why is this true? a[5] == 5[a]
Someone told me this... I didn't believe them at first but it does work. If x and y do not change throughout the code, why does this work:
int x [5] = { 0,1,2,3,4};
int y = 3;
if(x[y] == y[x]){
cout << "Why..." << endl;
}
How does x array's value in index y is = the x index's value's in array y? But there was no y array.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它始终为真(对于普通运算符==),
因为 int 是内在的并且具有可交换运算符==,这将始终为真
It is always true (for normal operator==)
since int is intrinsic and has commutative operator==, this will always be true
因为以下所有内容都是相同的:
Because all of the following are same:
因为
x[y]
只是*(x + y)
的另一种表达方式,与*(y + x)
相同>。Because
x[y]
is just another way to say*(x + y)
, and that is the same as*(y + x)
.