Man Page iterator_traits.3



                       Standard C++ Library
             Copyright 1998, Rogue Wave Software, Inc.



NAME

     iterator_traits

      - Returns basic information about an iterator.





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 algo-
     rithms  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 effi-
     ciency by using the tag to select from one of  several  dif-
     ferent   auxiliary  functions.  The  iterator_category  must
     therefore be one  of  the  iterator  tags  included  by  the
     library.





TAG TYPES

     input_iterator_tag
     output_iterator_tag
     forward_iterator_tag
     bidirectional_iterator_tag
     random_access_iterator_tag

     iterator_traits::iterator_category is  typically  used  like
     this:


     template <class Iterator>
     void foo(Iterator first, Iterator last)
     {
      __foo(begin,end,
            iterator_traits<Iterator>::iterator_category);
     }

     template <class Iterator>
     void __foo(Iterator first, Iterator last,
               input_iterator_tag>
     {
       // Most general implementation
     }

     template <class Iterator>
     void __foo(Iterator first, Iterator last,
               bidirectional_iterator_tag>
     {
       // Implementation takes advantage of bi-directional
       // capability of the iterators
     }


     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,   dis-
     tance, advance, iterator