ThomasSoloist 于 2014.11.06 22:28 提问

C++的数组，为什么a[i]=i[a]，求解释

int a[5]={1,2,3,4,5}

2个回答

mymtom   2014.11.10 16:25

E1[E2]

(*(E1+(E2)))

3.5.4.2 Array declarators

Constraints

The expression that specifies the size of an array shall be an
integral constant expression that has a value greater than zero.

Semantics

If, in the declaration `` T D1 ,'' D1 has the form

``````     D[ constant-expression<opt>]
``````

3.3.2.1 Array subscripting

Constraints

``````**One of the expressions shall have type ``pointer to object type ,''
``````

the other expression shall have integral type, and the result has type
`` type .''**

Semantics

A postfix expression followed by an expression in square brackets
[] is a subscripted designation of a member of an array object. The
definition of the subscript operator [] is that E1[E2] is identical to
(*(E1+(E2))) . Because of the conversion rules that apply to the
binary + operator, if E1 is an array object (equivalently, a pointer
to the initial member of an array object) and E2 is an integer, E1[E2]
designates the E2 -th member of E1 (counting from zero).

happyparrot      2014.11.07 09:26

a是一个地址值，3是一个偏移量值。a[3]和3[a]都相当于这两个值相加，所以结果都指向同一个地址。
C++中数组与指针在很多方面是一致的，有些类似于相同内容的不同展示方式，但细节上还是有所区别的。

Problem G: 求二维数组中的鞍点【数组】

nlongn 逆序对 外加归并的讲解

1.问题描述　　在一个整数数组中，元素都为整数，没有重复数。设计一个算法找出满足两个数的和等于k值得数对。例如a[]={1,3,8,6,4}中两个数的和为7的数对为（1，6）和（3，4）。2. 解决方案2.1 暴力法　　首先先到的可能就是暴力法，暴力没举出所有的数对然后再判对他们的和是否为K，但这种方法的时间复杂度为O（n^2），效率比较低，一般不可取。代码也就不写了。、2.2 二分法　　先对数组进

Problem B: 求二维数组中的鞍点【数组】

uva 11424 GCD Extreme 求∑∑gcd(i,j) (1<i<n,i<j<n)
<br />Problem H<br />GCD Extreme<br />Input: Standard Input<br />Output: Standard Output <br /> <br />Given the value of N, you will have to find the value of G. The definition of G is given below:<br /><br /> <br />Here GCD(i,j) means the greatest common
[360面试题2018]：一个数组a[0...n-1]，求a[j]-a[i]的最大值+求a[i]-a[j]的最大值，其中i<j

Lintcode-贪心-最大子数组和最小子数组