1 / 14

Iterators

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).

neron
Download Presentation

Iterators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Iterators Andy Wang Data Structures, Algorithms, and Generic Programming

  2. Iterators Overview • Provide access to container elements • Provide interface between generic algorithms and containers • Proper type • Uniform public interface across variety of containers

  3. 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

  4. 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&);

  5. Forward Iterators • Minimum public interface Container::Iterator I; for (I = C.Begin(); I != C.End(); ++I) { cout << *I; }

  6. Bidirectional Iterators • Forward iterator with more motion // motion Iterator& operator++(); Iterator& operator++(int); Iterator& operator--(); Iterator& operator--(int);

  7. 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

  8. 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

  9. 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++; } }

  10. 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

  11. The Iterator Hierarchy • Enforced by discipline in design • Not inheritance Input Iterators Output Iterators Forward Iterators Bindirectional Iterators Random Access Iterators

  12. 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

  13. 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

  14. 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);

More Related