[ fromfile: iterators.xml id: iterators ]
Any time you have a container of Thing
s, sooner or later, you need to loop through the container and do something with (or to) each individual 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 supports the following styles of iteration:
Qt style foreach
loops, similar to Perl and Python
Java 1.2 style Iterator
, which always points between elements
Standard Library style ContainerType::iterator
Hand-made while
or for
loops that use the getters of the container
QDirIterator, for iterating through entries in a directory structure
STL-style iterators behave more like pointers than do Java-style iterators. One important difference between iterators and pointers is that there is no iterator value that corresponds to the null value for pointers. For example, a function that uses pointers to search for something in a collection of items can return a null pointer if the search fails. There is no corresponding, generally recognizable iterator value to return that would indicate an unsuccessful iterator-based search.
Section 4.2.1 demonstrates the various styles of iteration available in C++ with Qt.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |