iterator_tags
SummaryIterator tag functions are a method for accessing information that is associated with iterators. Specifically, an iterator type must, as discussed in the InputIterator requirements, have an associated distance type and value type. [1] It is sometimes important for an algorithm parameterized by an iterator type to be able to determine the distance type and value type. Iterator tags also allow algorithms to determine an iterator's category, so that they can take different actions depending on whether an iterator is an InputIterator, OutputIterator, ForwardIterator, BidirectionalIterator, or RandomAccessIterator.
Note that the iterator tag functions DescriptionThe basic idea of the iterator tag functions, and ofiterator_traits , is quite simple: iterators have associated type information, and there must be a way to access that information. Specifically, iterator tag functions and iterator_traits are used to determine an iterator's value type, distance type, and iterator category.
An iterator's category is the most specific concept that it is a model of: InputIterator, OutputIterator, ForwardIterator, BidirectionalIterator, or RandomAccessIterator. This information is expressed in the C++ type system by defining five category tag types,
The function
An iterator's value type is the type of object that is returned when the iterator is dereferenced. (See the discussion in the InputIterator requirements.) Ideally, one might want
(Note that the function
An iterator's distance type, or difference type (the terms are synonymous) is the type that is used to represent the distance between two iterators. (See the discussion in the InputIterator requirements.) The function
Just as with
The functions
(Again, note that base classes are provided solely for the convenience of people who define iterators. If you define a class ExamplesThis example uses thevalue_type iterator tag function in order to declare a temporary variable of an iterator's value type. Note the use of an auxiliary function, __iter_swap . This is a very common idiom: most uses of iterator tags involve auxiliary functions. template <class ForwardIterator1, class ForwardIterator2, class ValueType> inline void __iter_swap(ForwardIterator1 a, ForwardIterator2 b, ValueType*) { ValueType tmp = *a; *a = *b; *b = tmp; } template <class ForwardIterator1, class ForwardIterator2> inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) { __iter_swap(a, b, value_type(a)); }
This example does exactly the same thing, using template <class ForwardIterator1, class ForwardIterator2> inline void iter_swap(ForwardIterator1 a, ForwardIterator2 b) { iterator_traits<ForwardIterator1>::value_type tmp = *a; *a = *b; *b = tmp; }
This example uses the template <class BidirectionalIterator> void __reverse(BidirectionalIterator first, BidirectionalIterator last, bidirectional_iterator_tag) { while (true) if (first == last || first == --last) return; else iter_swap(first++, last); } template <class RandomAccessIterator> void __reverse(RandomAccessIterator first, RandomAccessIterator last, random_access_iterator_tag) { while (first < last) iter_swap(first++, --last); } template <class BidirectionalIterator> inline void reverse(BidirectionalIterator first, BidirectionalIterator last) { __reverse(first, last, iterator_category(first)); }
In this case, template <class BidirectionalIterator> inline void reverse(BidirectionalIterator first, BidirectionalIterator last) { __reverse(first, last, iterator_traits<first>::iterator_category()); } ConceptsTypes
FunctionsNotes[1] OutputIterator have neither a distance type nor a value type; in many ways, in fact, OutputIterator aren't really iterators. Output iterators do not have a value type, because it is impossible to obtain a value from an output iterator but only to write a value through it. They do not have a distance type, similarly, because it is impossible to find the distance from one output iterator to another. Finding a distance requires a comparison for equality, and output iterators do not supportoperator== .
[2] The [3] Note that trivial does not appear in this list. The trivial concept is introduced solely for conceptual clarity; the STL does not actually define any trivial types, so there is no need for a trivial tag. There is, in fact, a strong reason not to define one: the C++ type system does not provide any way to distinguish between a pointer that is being used as a trivial iterator (that is, a pointer to an object that isn't part of an array) and a pointer that is being used as a RandomAccessIterator into an array. See alsoInputIterator, OutputIterator, ForwardIterator, BidirectionalIterator, RandomAccessIterator,iterator_traits , Iterators |