150 likes | 376 Views
Iterators. Andy Wang Data Structures, Algorithms, and Generic Programming. Iterators Overview. Provide access to container elements Provide interface between generic algorithms and containers Proper type Uniform public interface across variety of containers. Iterators Overview (2).
E N D
Iterators Andy Wang Data Structures, Algorithms, and Generic Programming
Iterators Overview • Provide access to container elements • Provide interface between generic algorithms and containers • Proper type • Uniform public interface across variety of containers
Iterators Overview (2) • Classify iterators by public interface functionality • input and output iterators • forward iterators • bidirectional iterators • random access iterators • adaptor iterators • Specialize iterators to containers with implementation
Forward Iterators // comparison logic bool operator==(const Iterators &) const; bool operator!=(const Iteartor&) const; // element access value_type& operator*() const; // motion Iterator& operator++(); Iterator& operator++(int); // proper type Iterator(); ~Iterator(); Iterator& Iterator(const Iterator&); Iteartor& operator=(const Iterator&);
Forward Iterators • Minimum public interface Container::Iterator I; for (I = C.Begin(); I != C.End(); ++I) { cout << *I; }
Bidirectional Iterators • Forward iterator with more motion // motion Iterator& operator++(); Iterator& operator++(int); Iterator& operator--(); Iterator& operator--(int);
Random Access Iterators • Bidirectional iterator with random accesses // bracket operator T& operator[] (unsigned int i) const; // pointer arithmetics long operator-(const Iterator I2) const; // n = I – I2 Iterator operator+(long n) const; // I2 = I + n Iterator& operator+=(long n); Iterator& operator-=(long n); // similar declarations of last three for all other integral types
Ok for v = *I; not ok for *I = v; Input Iterators • Specialized read-only forward iterator // comparison logic bool operator==(const Iterators &) const; bool operator!=(const Iteartor&) const; // element access const value_type& operator*() const; // motion Iterator& operator++(); Iterator& operator++(int); // proper type Iterator(); ~Iterator(); Iterator& Iterator(const Iterator&); Iteartor& operator=(const Iterator&); // no assignment operator
Example • g_copy template <class I, class J> void g_copy(I source_begin, I source_end, J dest_begin) { while (source_begin != source_end) { *dest_begin++ = *source_begin++; } }
Output Iterators • Specialized write-only forward iterator // no comparison logic // write-only element access Iterator& operator*() const; // motion Iterator& operator++(); Iterator& operator++(int); // proper type Iterator(); ~Iterator(); Iterator& Iterator(const Iterator&); // no assignment operator
The Iterator Hierarchy • Enforced by discipline in design • Not inheritance Input Iterators Output Iterators Forward Iterators Bindirectional Iterators Random Access Iterators
Iterator Adaptors • Insert iterators • Adaptee: a container class • Adaptor: an output iterator • Utility: replace overwrite with insert in generic algorithms • Stream iterators • Adaptee: an istream/ostream class • Adaptor: an input/output iterator • Utility: read/write directly from/to istream/ostream in generic algorithms
Iterator Adaptors (2) • Reverse iterators • Adaptee: an iterator class • Adaptor: an iterator of the same category • Utility: normal direction of increment/decrement is reversed—applies generic algorithms in reverse direction
Insert Iterators template <class C> class PushBackIterator { public: explicit PushBackIterator(C& x):Cptr(&x) {} PushBackIterator<C>& operator=(const typename C::value_type & t) { Cptr->PushBack(t); return *this; } PushBackIterator<C>& operator*() { return *this; } PushBackIterator<C>& operator++() { return *this; } PushBackIterator<C>& operator++(int) {return *this; } protected: C* Cptr; }; // usage TList<char> L; PushBackIterator <TList <char> > Litr(L); g_copy(V.Begin(), V.End(), Litr);