Rogue Wave banner
Previous fileTop of documentContentsIndexNext file

iterator_traits


Iterator traits class

Summary

Returns basic information about an iterator.

Data Type and Member Function Indexes
(exclusive of constructors and destructors)

None

Synopsis

template <class Iterator> struct iterator_traits
{
   typedef typename Iterator::value_type value_type;
   typedef typename Iterator::difference_type
                              difference_type;
   typedef typename Iterator::pointer pointer;
   typedef typename Iterator::reference reference;
   typedef typename Iterator::iterator_category
                              iterator_category;
};

// Specialization
template <class T> struct iterator_traits<T*>
{
   typedef T value_type;
   typedef ptrdiff_t difference_type;
   typedef T* pointer;
   typedef T& reference;
   typedef random_access_iterator_tag iterator_category;
};

Description

The iterator_traits template and specialization allows algorithms to access information about a particular iterator in a uniform way. The template requires either an iterator with a basic interface consisting of the types value_type, difference_type, pointer, reference, and iterator_category, or it requires a specialization for the iterator. The library includes one specialization (partial) to handle all pointer iterator types.

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. An algorithm can use this "tag" to select the most efficient implementation an iterator is capable of handling without sacrificing the ability to work with a wide range of iterator types. For instance, both the advance and distance primitives use iterator_category to maximize their efficiency by using the tag to select from one of several different auxiliary functions. The iterator_category must therefore be one of the iterator tags included by the library.

Tag Types

iterator_traits::iterator_category is typically used like this:

See the iterator section for a description of iterators and the capabilities associated with each type of iterator tag.

Warnings

If your compiler does not support partial specialization, then this template and specialization are not available to you. Instead you need to use the __distance_type, __value_type, and __iterator_category families of function templates. The Rogue Wave Standard C++ Library also includes alternate implementations of the distance, advance, and count functions when partial specialization is not supported by a particular compiler.

If your compiler does not support namespaces, then you do not need the using declaration for std.

See Also

__value_type, __distance_type, __iterator_category, distance, advance, iterator



Previous fileTop of documentContentsIndexNext file
©Copyright 1998, Rogue Wave Software, Inc.
Send mail to report errors or comment on the documentation.
OEM Release, June 1998