Basilevs
Basilevs

Reputation: 23916

How input_iterator_tag is different from forward_iterator_tag?

How std::input_iterator_tag is different from std::forward_iterator_tag?

Inspired by SO answers about C++ iterators. Both tags seem to be appropriate in same cases.

Upvotes: 6

Views: 1115

Answers (1)

Mark Storer
Mark Storer

Reputation: 15868

  1. You can set values through a forward iterator. *iter = foo; is legal in an output iterator, but not an input iterator, whereas a forward iterator can both read and write, unless it is immutable.

    const SinglelyLinkedList myList = foo();
    // a const container should return immutable iterators
    SomeIterTypedef immutableIter = myList.begin();
    
  2. An input iterator can wrap the output of a function. Forward iterators "can be used in multi-pass algorithms". Two copies of a forward iterator should produce the same results unless the underlying container changes. Input iterators don't even have to be associated with a container... istream_iterator for example.

I distilled all that from the SGI iterators page and the specific input, output, and forward iterator pages.

Upvotes: 7

Related Questions