4.2.  Iterators

[ fromfile: iterators.xml id: iterators ]

Any time you have a container of Things, 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:

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.



[36] We discuss design patterns in more detail in Section 7.4.