//Listing 4: input_iterator.h #ifndef INPUT_ITERATOR_H #define INPUT_ITERATOR_H #include #ifndef BRAIN_DEAD_COMPILER #include "pointer_traits.h" #endif #include #ifndef BRAIN_DEAD_COMPILER template #else template #endif class input_iter_adaptor : #ifndef BRAIN_DEAD_COMPILER public std::iterator< std::input_iterator_tag, typename pointer_traits

::element_type::value_type, Distance > #else public input_iterator #endif { public: typedef input_iter_adaptor self; #ifndef BRAIN_DEAD_COMPILER typedef typename pointer_traits

::element_type::deref_type deref_type; #else typedef Der deref_type; #endif class PIR { public: typedef typename input_iter_adaptor::value_type value_type; PIR(value_type const & x) : value(x) { } // Default ctor, dtor, assign value_type operator*() const { return value; } private: value_type value; }; explicit input_iter_adaptor(P const & pg) : p(pg) { inc(); } input_iter_adaptor() // past-the-end value; non-dereferenceable : p(0) { } // Default copy ctor, assignment, dtor. bool valid() const // *this != self() { return p != P(0); } deref_type operator*() const { return p->value(); } value_type const * operator->() const { return &(p->value()); } self& operator++() { inc(); return *this; } PIR operator++(int) { PIR tmp( p->value() ); inc(); return tmp; } bool operator==(self const & x) const { return p == x.p; } bool operator!=(self const & x) const { return p != x.p; } private: P p; void inc() { if (!p->next()) p = P(0); } }; #endif