[ fromfile: iterators.xml id: qstringlist ]
For text processing, it is useful to work with lists of strings.
A QStringList is actually a QList<QString> so you can make use of the
QList public interface.[37]
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/containers/lists/lists-examples.cpp
#include <QStringList> #include <QDebug> /* 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; list += spring; list.append(summer); list << fall; qDebug() << "The Spring months are: " << list[1] ; QString allmonths = list.join(", "); qDebug() << allmonths; QStringList list2 = allmonths.split(", "); Q_ASSERT(list2.size() == 12); foreach (const QString &str, list) { qDebug() << QString(" [%1] ").arg(str); } for (QStringList::iterator it = list.begin(); it != list.end(); ++it) { QString current = *it; qDebug() << "[[" << current << "]]"; } QListIterator<QString> itr (list2); while (itr.hasNext()) { QString current = itr.next(); qDebug() << "{" << current << "}"; } return 0; }
Append operator 1. |
|
Append operator 2. |
|
Append member function. |
|
From list to string - join with a ", " delimiter. |
|
Split is the opposite of join. Each month will have its own element. |
|
Q_ASSERTions abort the program if the condition is not satisfied. |
|
Qt foreach loop - similar to Perl/Python and Java 1.5 style for loops. |
|
C++ STL-style iteration. |
|
Pointer-style dereference. |
|
Java 1.2 style Iterator. |
|
Java Iterators point between elements. |
Following is the output of Example 4.1:
src/containers/lists> ./lists 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] " [[ "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/containers/lists>
Qt tries to accommodate programmers who have various habits and styles.
For example, QList::Iterator
is just a typedef
(alias) for QList::iterator
,
providing two different ways to refer to the STL-style iterator
class.
The QListIterator and
QMutableListIterator classes provide Java-style iterators that point between list elements and access particular elements with
previous()
and next()
.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |