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:
The next section demonstrates the various styles of iteration available in C++ with Qt 4.
4.2.1. QStringList and Iteration
For text processing, it is very useful to work with lists of strings. QStringList is derived from QList so it inherits all of QList's behavior (see Chapter 6). In addition, QStringList has some string-specific convenience functions such as indexOf(), join(), and replaceInStrings().
Converting between lists and individual strings is quite easy with perl-like split() and join() functions. Example 4.1 demonstrates lists, iterations, split(), and join().
Example 4.1. src/collections/lists/lists-examples.cpp
#include #include #include /* Some simple examples using QStringList, split and join */ int main() { QString winter = "December, January, February"; QString spring = "March, April, May"; QString summer = "June, July, August"; QString fall = "September, October, November"; QStringList list; list << winter; <-- 1 list += spring; <-- 2 list.append(summer); <-- 3 list << fall; qDebug() << "The Spring months are: " << list[1] ; QString allmonths = list.join(", "); /* from list to string - join with a ", " delimiter */ qDebug() << allmonths; QStringList list2 = allmonths.split(", "); /* split is the opposite of join. Each month will have its own element. */ assert(list2.size() == 12); <-- 4 foreach (QString str, list) { <-- 5 qDebug() << QString(" [%1] ").arg(str); } for (QStringList::iterator it = list.begin(); it != list.end(); ++it) { <-- 6 QString current = *it; <-- 7 qDebug() << "[[" << current << "]]"; } QListIterator itr (list2); <-- 8 while (itr.hasNext()) { <-- 9 QString current = itr.next(); qDebug() << "{" << current << "}"; } return 0; }
|
src/collections> ./collections The Spring months are: March, April, May December, January, February, March, April, May, June, July, August, September, October, November [December] [January] [February] [March] [April] [May] [June] [July] [August] [September] [October] [November] src/collections>
Relationships |
Part I: Introduction to C++ and Qt 4
C++ Introduction
Classes
Introduction to Qt
Lists
Functions
Inheritance and Polymorphism
Part II: Higher-Level Programming
Libraries
Introduction to Design Patterns
QObject
Generics and Containers
Qt GUI Widgets
Concurrency
Validation and Regular Expressions
Parsing XML
Meta Objects, Properties, and Reflective Programming
More Design Patterns
Models and Views
Qt SQL Classes
Part III: C++ Language Reference
Types and Expressions
Scope and Storage Class
Statements and Control Structures
Memory Access
Chapter Summary
Inheritance in Detail
Miscellaneous Topics
Part IV: Programming Assignments
MP3 Jukebox Assignments
Part V: Appendices
MP3 Jukebox Assignments
Bibliography
MP3 Jukebox Assignments