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.
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.
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.
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.
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();
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With