不表示序列结束的哨兵的术语是什么?
What is the term for a sentinel that does not denote the end of a sequence?
根据 Wikipedia,
In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.
一个常见的例子是当数组大于填充它的数据时,在正整数数组的末尾使用 -1
来表示数据的结束。
但是,当我们使用 -1
不是为了确保终止,而是仅仅作为一个不可能的值时呢?例如:
# `a` is an array with the numbers from 0 to 4, in random order.
# We will use `b` to track where in `a` each number is. The position in `b`
# denotes the number, and the value denotes the index in `a`.
# `calculate_indices()` is a function that does this.
a = [3, 4, 1, 0, 2]
b = [-1, -1, -1, -1, -1]
calculate_indices(a, b)
print(b) => [3, 2, 4, 0, 1]
如果我们要将 b
初始化为 [0, 0, 0, 0, 0]
,这是行不通的,因为 0
在这里具有实际意义 - 即 a
中的第 0 个位置.因此我们使用一个不可能的值,-1
.
这种用法有名称吗?
根据 Wikipedia,
In computer programming, a sentinel value (also referred to as a flag value, trip value, rogue value, signal value, or dummy data) is a special value in the context of an algorithm which uses its presence as a condition of termination, typically in a loop or recursive algorithm.
一个常见的例子是当数组大于填充它的数据时,在正整数数组的末尾使用 -1
来表示数据的结束。
但是,当我们使用 -1
不是为了确保终止,而是仅仅作为一个不可能的值时呢?例如:
# `a` is an array with the numbers from 0 to 4, in random order.
# We will use `b` to track where in `a` each number is. The position in `b`
# denotes the number, and the value denotes the index in `a`.
# `calculate_indices()` is a function that does this.
a = [3, 4, 1, 0, 2]
b = [-1, -1, -1, -1, -1]
calculate_indices(a, b)
print(b) => [3, 2, 4, 0, 1]
如果我们要将 b
初始化为 [0, 0, 0, 0, 0]
,这是行不通的,因为 0
在这里具有实际意义 - 即 a
中的第 0 个位置.因此我们使用一个不可能的值,-1
.
这种用法有名称吗?