【求助】关于c++的一个程序,敬请各位大神解答

孤傲猎人 2016-11-03 09:38:12
int a[]={1,2,3,4,5};
cout<<(3+1)[a]<<endl;

这两句语句运行后输出5,但是我不明白(3+1)[a]是什么意思,这是一个什么语句,是怎样的功能呢?各位大神,求解答!
...全文
115 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-11-04
  • 打赏
  • 举报
回复
A[B]
*(A+B)
*(B+A)
B[A]
//以上四行等价

"abc"[1]
*("abc"+1)
*(1+"abc")
1["abc"]
//以上四行等价

A[0]
*(A)
0[A]
//以上三行等价
小灸舞 2016-11-04
  • 打赏
  • 举报
回复
(3+1)[a] = a[3+1] = a[4] = (4)[a]
fefe82 2016-11-04
  • 打赏
  • 举报
回复
C++标准草案n4296 5.2.1 Subscripting [expr.sub] 1 A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type “array of T” or “pointer to T” and the other shall have unscoped enumeration or integral type. The result is of type “T.” The type “T” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 5.3 and 5.7 for details of * and + and 8.3.4 for details of arrays. — end note ], except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise. 内置的 [] 。 重载的是函数调用,不在此列。
孤傲猎人 2016-11-04
  • 打赏
  • 举报
回复
十分感谢!
引用 6 楼 fefe82 的回复:
C++标准草案n4296 5.2.1 Subscripting [expr.sub] 1 A postfix expression followed by an expression in square brackets is a postfix expression. One of the expressions shall have the type “array of T” or “pointer to T” and the other shall have unscoped enumeration or integral type. The result is of type “T.” The type “T” shall be a completely-defined object type. The expression E1[E2] is identical (by definition) to *((E1)+(E2)) [ Note: see 5.3 and 5.7 for details of * and + and 8.3.4 for details of arrays. — end note ], except that in the case of an array operand, the result is an lvalue if that operand is an lvalue and an xvalue otherwise. 内置的 [] 。 重载的是函数调用,不在此列。
孤傲猎人 2016-11-03
  • 打赏
  • 举报
回复
我也是这么猜的,但是我找不到这样的说法啊,哥们,你能找到相关的论据吗,或者在某些资料上看到过,谢谢啦,如果有,可不可以解答一下,十分感谢!
paschen 版主 2016-11-03
  • 打赏
  • 举报
回复
paschen 版主 2016-11-03
  • 打赏
  • 举报
回复
(3+1)[a] 与 a[3+1] 等价
孤傲猎人 2016-11-03
  • 打赏
  • 举报
回复
我是第一次遇到这种情况,真是奇了怪了,我在想,a[4]与(4)[a]是有什么关系吗,网上也查不到相关信息
海底捞哥 2016-11-03
  • 打赏
  • 举报
回复
厉害了,word 哥,居然编译成功了。。。

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧