Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How input_iterator_tag is different from forward_iterator_tag?

Tags:

stl

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.

like image 805
Basilevs Avatar asked Nov 01 '10 14:11

Basilevs


People also ask

What is Forward_iterator_tag C++?

Forward_iterator_tag is an empty class: it has no member functions, member variables, or nested types. It is used solely as a "tag": a representation of the Forward Iterator concept within the C++ type system. Specifically, it is used as a return value for the function iterator_category.

What are iterator traits?

iterator_traits are used within algorithms to create local variables of either the type pointed to by the iterator or of the iterator's distance type. The traits also improve the efficiency of algorithms by making use of knowledge about basic iterator categories provided by the iterator_category member.

What is iterator tag?

Summary. Iterator tag functions are a method for accessing information that is associated with iterators. Specifically, an iterator type must, as discussed in the Input Iterator requirements, have an associated distance type and value type.


1 Answers

  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.

like image 165
Mark Storer Avatar answered Oct 04 '22 21:10

Mark Storer