gpt4 book ai didi

c++ - "[ptr, ptr+len) must be a valid range"是什么意思?

转载 作者:搜寻专家 更新时间:2023-10-31 01:29:11 24 4
gpt4 key购买 nike

在 c++ 标准的不同地方,短语:

要求: [p, p + len) 是一个有效范围。

(其中 p 是一个指针,len 是一个数字)被使用。

迭代器的替代版本是:

要求: [first, last) 是一个有效范围。

这是什么意思?

最佳答案

我认为 C++17 标准的相关部分在这里

27.2.1 In general [ iterator.requirements.general ]

...

8 An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j. If j is reachable from i, they refer to elements of the same sequence.

9 Most of the library’s algorithmic templates that operate on data structures have interfaces that use ranges. A range is a pair of iterators that designate the beginning and end of the computation. A range [i, i) is an empty range; in general, a range [i, j) refers to the elements in the data structure starting with the element pointed to by i and up to but not including the element pointed to by j. Range [i, j) is valid if and only if j is reachable from i. The result of the application of functions in the library to invalid ranges is undefined.

所以提取相关部分:

8 An iterator j is called reachable from an iterator i if and only if there is a finite sequence of applications of the expression ++i that makes i == j...

9 ... Range [i, j) is valid if and only if j is reachable from i....

因此,“有效范围”似乎是属于同一容器(或数组)的一对迭代器,具有ji 更远。

但也不要忘记:

in general, a range [i, j) refers to the elements in the data structure starting with the element pointed to by i and up to but not including the element pointed to by j.

因此 ij 之间的所有迭代器(但不一定包括 j)必须“在数据结构中”(即不越界)

关于c++ - "[ptr, ptr+len) must be a valid range"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50454342/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com