Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...