输入迭代器和只读前向迭代器有什么区别?

发布于 2024-12-26 17:36:24 字数 147 浏览 1 评论 0原文

输入迭代器和只读前向迭代器有什么区别?

因为后者是只读的,所以它们显然不能满足输出迭代器的要求。并且,正因为如此,它们实际上是具有额外保证(如果有)的输入迭代器。问题是,还有什么额外保障呢?

我的猜测是,前向迭代器是多遍的,而输入迭代器不是,对吗?

What's the difference between input iterators and read-only forward iterators?

Because the latter are read-only, they obviously don't satisfy requirements of output iterators. And, because of that, they're effectively input iterators with additional guarantees (if any). The problem is, what additional guarantees?

My guess would be that forward iterators are multi-pass and input iterators are not, am I right?

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

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

发布评论

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

评论(1

踏雪无痕 2025-01-02 17:36:24

是的,输入迭代器是一次性迭代器。您只能对它们进行一次迭代,而前向迭代器可以多次迭代。

来自§24.2.3 [input.iterators] p2(表)++r 的前置/后置条件列:

pre: r 是可解引用的。
帖子:r 是可取消引用的,或者 r 是最后的。
post:r 先前值的任何副本不再需要可取消引用或位于 == 域中。< /p>

最后一个后置条件意味着对于 a == b++a == ++b 不需要为 true
同一条,第 3 段:

[ 注意: 对于输入迭代器,a == b 并不意味着 ++a == ++b。 (相等并不能保证替换属性或引用透明度。)输入迭代器上的算法不应尝试两次通过同一迭代器。它们应该是单通道算法。 [...] 这些算法可以通过 istream_iterator 类模板与 istream 一起用作输入数据源。 —尾注]

来自 §24.2.5 [forward.iterators]

p1 类或指针类型 X 满足前向迭代器的要求,如果

  • [...]
  • X 类型的对象提供多遍保证,如下所述。

p3 两个 X 类型的可解引用迭代器 a 和 b 提供多遍保证,如果:

  • a == b 意味着 ++a == ++b
  • X 是指针类型或表达式 (void)++X(a), *a 等价于表达式 *a.

Yes, input iterators are one-pass iterators. You can only iterate over them once, while forward iterators are multi-pass.

From §24.2.3 [input.iterators] p2 (the table), pre-/postcondition column of ++r:

pre: r is dereferenceable.
post: r is dereferenceable or r is past-the-end.
post: any copies of the previous value of r are no longer required either to be dereferenceable or to be in the domain of ==.

The last postcondition implies that for a == b, ++a == ++b is not required to be true.
Same clause, paragraph 3:

[ Note: For input iterators, a == b does not imply ++a == ++b. (Equality does not guarantee the substitution property or referential transparency.) Algorithms on input iterators should never attempt to pass through the same iterator twice. They should be single pass algorithms. [...] These algorithms can be used with istreams as the source of the input data through the istream_iterator class template. —end note ]

From §24.2.5 [forward.iterators]

p1 A class or pointer type X satisfies the requirements of a forward iterator if

  • [...]
  • objects of type X offer the multi-pass guarantee, described below.

p3 Two dereferenceable iterators a and b of type X offer the multi-pass guarantee if:

  • a == b implies ++a == ++b and
  • X is a pointer type or the expression (void)++X(a), *a is equivalent to the expression *a.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文