golang数组引用例如。 b[1:4] 引用元素 1,2,3

发布于 2024-11-04 14:31:52 字数 203 浏览 1 评论 0原文

golang 博客指出:

“切片也可以通过“切片”现有切片或数组来形成。切片是通过指定一个半开范围来完成的,其中两个索引由冒号分隔。例如,表达式 b[1:4 ] 创建一个包含 b 的元素 1 到 3 的切片(结果切片的索引将为 0 到 2)。”

有人可以向我解释一下上面的逻辑吗? IE。为什么 b[1:4] 不引用元素 1 到 4?这与其他数组引用一致吗?

The golang blog states :

"A slice can also be formed by "slicing" an existing slice or array. Slicing is done by specifying a half-open range with two indices separated by a colon. For example, the expression b[1:4] creates a slice including elements 1 through 3 of b (the indices of the resulting slice will be 0 through 2)."

Can someone please explain to me the logic in the above. IE. Why doesn't b[1:4] reference elements 1 through 4? Is this consistent with other array referencing?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

毁梦 2024-11-11 14:31:52

索引指向元素的“开始”。这是所有使用从零开始索引的语言所共有的:

       | 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |
[0]   =  ^
[0:1] =  ^ --------> ^
[1:4] =              ^-------------------------------------> ^  
[0:5] =  ^ ----------------------------------------------------------> ^

支持负索引也很常见,尽管 Go 不允许这样做:

       |-6 |       |-5 |        |-4 |       |-3 |        |-2 |       |-1 |
       | 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |

Indexes point to the "start" of the element. This is shared by all languages using zero-based indexing:

       | 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |
[0]   =  ^
[0:1] =  ^ --------> ^
[1:4] =              ^-------------------------------------> ^  
[0:5] =  ^ ----------------------------------------------------------> ^

It's also common to support negative indexing, although Go doesn't allow this:

       |-6 |       |-5 |        |-4 |       |-3 |        |-2 |       |-1 |
       | 0 | first | 1 | second | 2 | third | 3 | fourth | 4 | fifth | 5 |
痞味浪人 2024-11-11 14:31:52

原因在 Go 语言规范的 Slices 部分给出。

对于字符串、数组或切片 a,
主要表达

<前><代码> a[低:高]

构造一个子字符串或切片。这
索引表达式 low 和 high 选择
哪些元素出现在结果中。
结果的索引从 0 开始
长度等于高 - 低。

为了方便起见,任何索引
表达式可以省略。一个失踪的
低指数默认为零;失踪的
高索引默认为长度
切片操作数。

计算切片长度的高 - 低非常简单且高效。

The reason is given in the Go Language Specification section on Slices.

For a string, array, or slice a, the
primary expression

    a[low : high]

constructs a substring or slice. The
index expressions low and high select
which elements appear in the result.
The result has indexes starting at 0
and length equal to high - low.

For convenience, any of the index
expressions may be omitted. A missing
low index defaults to zero; a missing
high index defaults to the length of
the sliced operand.

It's easy and efficient to calculate the length of the slice as high - low.

黑色毁心梦 2024-11-11 14:31:52

当你认真思考时,半开区间的意义有很多。例如,对于这样的半开区间,元素的数量是:

n = end - start

这是一个非常漂亮且简单的公式。对于闭区间,它将是:

n = (end - start) + 1

这(不是很多,但仍然)更复杂。

这也意味着对于例如一个字符串,整个字符串是 [1, len(s)] 这看起来也很直观。如果间隔是封闭的,要获取整个字符串,您将需要 [1, len(s) + 1]

Half-open intervals make sense for many reasons, when you get down to it. For instance, with a half-open interval like this, the number of elements is:

n = end - start

which is a pretty nice and easy formula. For a closed interval, it would be:

n = (end - start) + 1

which is (not a lot, but still) more complicated.

It also means that for e.g. a string, the entire string is [1, len(s)] which also seems intuitive. If the interval was closed, to get the entire string you would need [1, len(s) + 1].

赠佳期 2024-11-11 14:31:52

与许多其他语言一样,Go 对切片使用半开间隔。用更数学的表示法来说,切片 b[1:4] 是排除上端点的区间 [1,4)

Go uses half-open intervals for slices like many other languages. In a more mathematical notation, the slice b[1:4] is the interval [1,4) which excludes the upper endpoint.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文