Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
java-style-iterators.qdoc
Go to the documentation of this file.
1// Copyright (C) 2020 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page java-style-iterators.html
6 \title Java-style Iterators in Qt
7 \ingroup groups
8 \ingroup qt-basic-concepts
9 \brief Java style iterators for Qt's containers.
10
11 \section1 Java-Style Iterators
12
13 For each container class, there are two Java-style iterator data
14 types: one that provides read-only access and one that provides
15 read-write access.
16
17 \note New code should use \l{STL-Style iterators} since these are more efficient
18 and can be used together with Qt's and STL's \l{generic algorithms}.
19
20 \table
21 \header \li Containers \li Read-only iterator
22 \li Read-write iterator
23 \row \li QList<T>, QQueue<T>, QStack<T>, \li QListIterator<T>
24 \li QMutableListIterator<T>
25 \row \li QSet<T> \li QSetIterator<T>
26 \li QMutableSetIterator<T>
27 \row \li QMap<Key, T>, QMultiMap<Key, T> \li QMapIterator<Key, T>
28 \li QMutableMapIterator<Key, T>
29 \row \li QHash<Key, T>, QMultiHash<Key, T> \li QHashIterator<Key, T>
30 \li QMutableHashIterator<Key, T>
31 \endtable
32
33 In this discussion, we will concentrate on QList and QMap. The
34 iterator types for QSet have exactly
35 the same interface as QList's iterators; similarly, the iterator
36 types for QHash have the same interface as QMap's iterators.
37
38 Unlike \l{STL-Style iterators}, Java-style iterators point \e between items
39 rather than directly \e at items. For this reason, they are either pointing
40 to the very beginning of the container (before the first item), at the very
41 end of the container (after the last item), or between two items. The
42 diagram below shows the valid iterator positions as red arrows for a list
43 containing four items:
44
45 \image javaiterators1.png
46
47 Here's a typical loop for iterating through all the elements of a
48 QList<QString> in order:
49
50 \snippet code/doc_src_containers.cpp 1
51
52 It works as follows: The QList to iterate over is passed to the
53 QListIterator constructor. At that point, the iterator is located
54 just in front of the first item in the list (before item "A").
55 Then we call \l{QListIterator::hasNext()}{hasNext()} to
56 check whether there is an item after the iterator. If there is, we
57 call \l{QListIterator::next()}{next()} to jump over that
58 item. The next() function returns the item that it jumps over. For
59 a QList<QString>, that item is of type QString.
60
61 Here's how to iterate backward in a QList:
62
63 \snippet code/doc_src_containers.cpp 2
64
65 The code is symmetric with iterating forward, except that we
66 start by calling \l{QListIterator::toBack()}{toBack()}
67 to move the iterator after the last item in the list.
68
69 The diagram below illustrates the effect of calling
70 \l{QListIterator::next()}{next()} and
71 \l{QListIterator::previous()}{previous()} on an iterator:
72
73 \image javaiterators2.png
74
75 The following table summarizes the QListIterator API:
76
77 \table
78 \header \li Function \li Behavior
79 \row \li \l{QListIterator::toFront()}{toFront()}
80 \li Moves the iterator to the front of the list (before the first item)
81 \row \li \l{QListIterator::toBack()}{toBack()}
82 \li Moves the iterator to the back of the list (after the last item)
83 \row \li \l{QListIterator::hasNext()}{hasNext()}
84 \li Returns \c true if the iterator isn't at the back of the list
85 \row \li \l{QListIterator::next()}{next()}
86 \li Returns the next item and advances the iterator by one position
87 \row \li \l{QListIterator::peekNext()}{peekNext()}
88 \li Returns the next item without moving the iterator
89 \row \li \l{QListIterator::hasPrevious()}{hasPrevious()}
90 \li Returns \c true if the iterator isn't at the front of the list
91 \row \li \l{QListIterator::previous()}{previous()}
92 \li Returns the previous item and moves the iterator back by one position
93 \row \li \l{QListIterator::peekPrevious()}{peekPrevious()}
94 \li Returns the previous item without moving the iterator
95 \endtable
96
97 QListIterator provides no functions to insert or remove items
98 from the list as we iterate. To accomplish this, you must use
99 QMutableListIterator. Here's an example where we remove all
100 odd numbers from a QList<int> using QMutableListIterator:
101
102 \snippet code/doc_src_containers.cpp 3
103
104 The next() call in the loop is made every time. It jumps over the
105 next item in the list. The
106 \l{QMutableListIterator::remove()}{remove()} function removes the
107 last item that we jumped over from the list. The call to
108 \l{QMutableListIterator::remove()}{remove()} does not invalidate
109 the iterator, so it is safe to continue using it. This works just
110 as well when iterating backward:
111
112 \snippet code/doc_src_containers.cpp 4
113
114 If we just want to modify the value of an existing item, we can
115 use \l{QMutableListIterator::setValue()}{setValue()}. In the code
116 below, we replace any value larger than 128 with 128:
117
118 \snippet code/doc_src_containers.cpp 5
119
120 Just like \l{QMutableListIterator::remove()}{remove()},
121 \l{QMutableListIterator::setValue()}{setValue()} operates on the
122 last item that we jumped over. If we iterate forward, this is the
123 item just before the iterator; if we iterate backward, this is
124 the item just after the iterator.
125
126 The \l{QMutableListIterator::next()}{next()} function returns a
127 non-const reference to the item in the list. For simple
128 operations, we don't even need
129 \l{QMutableListIterator::setValue()}{setValue()}:
130
131 \snippet code/doc_src_containers.cpp 6
132
133 As mentioned above QSet's iterator
134 classes have exactly the same API as QList's. We will now turn to
135 QMapIterator, which is somewhat different because it iterates on
136 (key, value) pairs.
137
138 Like QListIterator, QMapIterator provides
139 \l{QMapIterator::toFront()}{toFront()},
140 \l{QMapIterator::toBack()}{toBack()},
141 \l{QMapIterator::hasNext()}{hasNext()},
142 \l{QMapIterator::next()}{next()},
143 \l{QMapIterator::peekNext()}{peekNext()},
144 \l{QMapIterator::hasPrevious()}{hasPrevious()},
145 \l{QMapIterator::previous()}{previous()}, and
146 \l{QMapIterator::peekPrevious()}{peekPrevious()}. The key and
147 value components are extracted by calling \l{QMapIterator::key()}{key()} and \l{QMapIterator::value()}{value()} on
148 the object returned by next(), peekNext(), previous(), or
149 peekPrevious().
150
151 The following example removes all (capital, country) pairs where
152 the capital's name ends with "City":
153
154 \snippet code/doc_src_containers.cpp 7
155
156 QMapIterator also provides a \l{QMapIterator::key()}{key()} and a \l{QMapIterator::value()}{value()} function that
157 operate directly on the iterator and that return the key and
158 value of the last item that the iterator jumped above. For
159 example, the following code copies the contents of a QMap into a
160 QHash:
161
162 \snippet code/doc_src_containers.cpp 8
163
164 If we want to iterate through all the items with the same
165 value, we can use \l{QMapIterator::findNext()}{findNext()}
166 or \l{QMapIterator::findPrevious()}{findPrevious()}.
167 Here's an example where we remove all the items with a particular
168 value:
169
170 \snippet code/doc_src_containers.cpp 9
171
172*/