Iterators - PowerPoint PPT Presentation

About This Presentation
Title:

Iterators

Description:

Ok for v = *I; not ok for *I = v; Example. g_copy. template class I, class J ... Adaptee: an iterator class. Adaptor: an iterator of the same category ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 15
Provided by: csF2
Learn more at: http://www.cs.fsu.edu
Category:
Tags: class | iterators

less

Transcript and Presenter's Notes

Title: Iterators


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
  • ContainerIterator I
  • for (I C.Begin() I ! C.End() I)
  • cout ltlt 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
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 ltclass I, class Jgt
  • 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 reversedapplies generic algorithms in reverse
    direction

14
Insert Iterators
  • template ltclass Cgt
  • class PushBackIterator
  • public
  • explicit PushBackIterator(C x)Cptr(x)
  • PushBackIteratorltCgt operator(const typename
    Cvalue_type t)
  • Cptr-gtPushBack(t)
  • return this
  • PushBackIteratorltCgt operator() return
    this
  • PushBackIteratorltCgt operator() return
    this
  • PushBackIteratorltCgt operator(int) return
    this
  • protected
  • C Cptr
  • // usage
  • TListltchargt L
  • PushBackIterator ltTList ltchargt gt Litr(L)
Write a Comment
User Comments (0)
About PowerShow.com