C++_STL_iterators - An iterator must be able to do 2 main...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 332: C++ STL iterators What is an Iterator? An iterator must be able to do 2 main things Point to the start of a range of elements Give (read and/or write) access to an element An iterator may be able to do 2 other important things Move to point to the next element in a range Be tested for having run through the entire range Intent (according to Gamma et al., “Design Patterns”) Provide a way to access the elements of a container sequentially without exposing its underlying representation Iterators help provide interface polymorphism Same interface, but different iterator implementations Due to underlying containers’ different structures We’ll consider iterator interfaces for istream, ostream, array, linked list, bi-linked list
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 332: C++ STL iterators STL Iterator Syntax and Semantics Pointers and integers support Gamma et al. iteration for (int * p = A; p – A < MAX; ++p) { cout << *p << endl; } for (unsigned int i = 0; i < MAX; ++i) { cout << A[i] << endl; } Pointers (but not integers) are also STL iterators Helped by traits based on typedefs, structs, specialization The compiler can only check the syntax Iterator definition must provide good semantics Including memory, aliasing, time complexity Why isn’t it ok to implement p += n via a for loop?
Background image of page 2
STL iterators Overview of STL Iterators STL iterators generalize different uses of pointers Interface between algorithms and data structures Algorithm manipulates iterators, not containers STL iterator’s value can represent 3 kinds of states : Dereferenceable (points to a valid location in a range) Past the end (points just past last valid location in a range) Singular (points to nothing) What’s an example of a pointer in each of these states? STL iterators may be compared for equality
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/03/2010 for the course CSE 332S taught by Professor Smart during the Spring '08 term at Washington University in St. Louis.

Page1 / 14

C++_STL_iterators - An iterator must be able to do 2 main...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online