Iterators
- container iterators
for(x.first(); x.good(); x.next())
process(x.current());
for(Elem * i = x.first(); i != 0; i = x.next())
process(*i);
often used in OpenDoc
- STL iterators
while( first != last ) process(*first++);
- natural iterators
Not-so-pleasant traits of the container/STL iterators
- have states
- potential danger
- accessing an iterator when it is not good
process(*first)
when first == last
- solution with pointers: real danger
- inefficiency
it takes a lot of trouble to make sure the iterator is safe to use:
three checks in the container iterator
iterators are distinguished by very characteristic patterns
much is written about what's neat about iterators. There is a talk about STL iterators here at MacHack. Let me mention a few not-so-neat traits
For example, a container iteration has to check three times if x is still good:
- before entering the body of the loop
- in accessing the current element
- before attempting to advance the iterator
Next | Table of contents