math - 从 0 开始数组的好处?

标签 math language-agnostic computer-science

在大多数编程语言中,从 0 开始的数组索引的目的是什么,与我们引用大多数 IRL 事物(第一、第二、第三等)的顺序方式相比?这背后的逻辑或效用是什么?

我现在已经完全习惯了,但从未停下来思考背后的原因。

更新 :我从谷歌上读到的一个好处是,如果你想升级到 i < n ,for 循环可以有 n

最佳答案

Dijkstra 在 Why numbering should start at zero 中列出了推理。

When dealing with a sequence of length N, the elements of which we wish to distinguish by subscript, the next vexing question is what subscript value to assign to its starting element...

when starting with subscript 1, the subscript range 1 ≤ i < N+1; starting with 0, however, gives the nicer range 0 ≤ i < N. So let us let our ordinals start at zero: an element's ordinal (subscript) equals the number of elements preceding it in the sequence. And the moral of the story is that we had better regard —after all those centuries!— zero as a most natural number.

关于math - 从 0 开始数组的好处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8526525/

相关文章:

java - 如果在 Android 中已知中心和起始坐标以及扫描角度,如何计算圆弧终点的坐标..?

math - 如果 f(n) = θ(g(n)),则 2^f(n) = θ(2^g(n)) 吗?

math - float 学有问题吗?

computer-science - 在屏幕上找到光标下对象的逻辑

computer-science - 什么是规律性?

c++ - 生成 4 个方向随机游动的 RCPP 函数

JavaScript 对象数组按属性的最小值过滤

language-agnostic - 动态调度实现

算法:选择集合的公共(public)元素

algorithm - N个圆的共同重叠