4.2.  Iterators

[ fromfile: iterators.xml id: iterators ]

Any time you have a container of things, sooner or later, you are probably going to loop through the container and do something with each thing. An iterator is an object that provides indirect access to each element in a container. It is specifically designed to be used in a loop.

Qt 4 supports the following styles of iteration:

  1. Qt 4 style foreach loops, similar to Perl and Python

  2. Java 1.2 style Iterator, which always point inbetween elements

  3. Standard Library style ContainerType::iterator

  4. Hand-made while or for loops that use getters of the container

  5. QDirIterator, for iterating through entries in a directory structure

The next section demonstrates the various styles of iteration available in C++ with Qt 4.