| | | | | |

ConstIterator Adaptor

  template < class I >
  class ConstRAIterator
  {
  protected:
    I i_;

  public:
    // terminology support
    typedef typename I::ValueType     ValueType;
    typedef typename I::PointerType   PointerType;
    typedef typename I::ConstPointerType   ConstPointerType;
    typedef typename I::ReferenceType ReferenceType;
    typedef typename I::ConstReferenceType ConstReferenceType;
    typedef typename I::ContainerType ContainerType;
    typedef typename I::Iterator      Iterator;
    typedef ConstRAIterator<I>        ConstIterator;

    ConstRAIterator  ();                         // default constructor
    ConstRAIterator  (const ConstRAIterator& i); // copy constructor
    ConstRAIterator  (const I& i);               // type converter

    bool                Valid     () const; // iterator is valid for dereference

    bool               operator == (const ConstIterator& i2) const;
    bool               operator != (const ConstIterator& i2) const;
    ConstReferenceType operator *  () const;             // return element as R-value
    ConstReferenceType operator [] (size_t index) const; // return element as R-value
    ConstIterator&     operator =  (const ConstIterator & i);
    ConstIterator&     operator ++ ();    // prefix increment
    ConstIterator      operator ++ (int); // postfix
    ConstIterator&     operator -- ();    // prefix decrement
    ConstIterator      operator -- (int); // postfix

    // "pointer" arithmetic

    long      operator -  (const ConstIterator & i2) const;

    // these are template member operators for pointer arithmetic

    template <typename N>
    ConstIterator  operator +  (N n) const;

    template <typename N>
    ConstIterator  operator -  (N n) const;

    template <typename N>
    ConstIterator& operator += (N n);

    template <typename N>
    ConstIterator& operator -= (N n);
  } ;

  // useage:

  template < typename T >
  bool IsIn (const List<T> & list, T searchVal)
  {
    List<T>::ConstIterator i; 
    for (i = list.Begin(); i != list.End(); ++i) // calls "const" versions of Begin, End
    if (searchVal == *i)
      return true;
    return false;
  }


| | Top of Page | 7. Iterators - 12 of 13