1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /*!
|
---|
43 | \group containers
|
---|
44 | \title Generic Containers
|
---|
45 | \ingroup architecture
|
---|
46 | \ingroup groups
|
---|
47 | \keyword container class
|
---|
48 | \keyword container classes
|
---|
49 |
|
---|
50 | \brief Qt's template-based container classes.
|
---|
51 |
|
---|
52 | \tableofcontents
|
---|
53 |
|
---|
54 | \section1 Introduction
|
---|
55 |
|
---|
56 | The Qt library provides a set of general purpose template-based
|
---|
57 | container classes. These classes can be used to store items of a
|
---|
58 | specified type. For example, if you need a resizable array of
|
---|
59 | \l{QString}s, use QVector<QString>.
|
---|
60 |
|
---|
61 | These container classes are designed to be lighter, safer, and
|
---|
62 | easier to use than the STL containers. If you are unfamiliar with
|
---|
63 | the STL, or prefer to do things the "Qt way", you can use these
|
---|
64 | classes instead of the STL classes.
|
---|
65 |
|
---|
66 | The container classes are \l{implicitly shared}, they are
|
---|
67 | \l{reentrant}, and they are optimized for speed, low memory
|
---|
68 | consumption, and minimal inline code expansion, resulting in
|
---|
69 | smaller executables. In addition, they are \l{thread-safe}
|
---|
70 | in situations where they are used as read-only containers
|
---|
71 | by all threads used to access them.
|
---|
72 |
|
---|
73 | For traversing the items stored in a container, you can use one
|
---|
74 | of two types of iterators: \l{Java-style iterators} and
|
---|
75 | \l{STL-style iterators}. The Java-style iterators are easier to
|
---|
76 | use and provide high-level functionality, whereas the STL-style
|
---|
77 | iterators are slightly more efficient and can be used together
|
---|
78 | with Qt's and STL's \l{generic algorithms}.
|
---|
79 |
|
---|
80 | Qt also offers a \l{foreach} keyword that make it very
|
---|
81 | easy to iterate over all the items stored in a container.
|
---|
82 |
|
---|
83 | \section1 The Container Classes
|
---|
84 |
|
---|
85 | Qt provides the following container classes:
|
---|
86 |
|
---|
87 | \table
|
---|
88 | \header \o Class \o Summary
|
---|
89 |
|
---|
90 | \row \o \l{QList}<T>
|
---|
91 | \o This is by far the most commonly used container class. It
|
---|
92 | stores a list of values of a given type (T) that can be accessed
|
---|
93 | by index. Internally, the QList is implemented using an array,
|
---|
94 | ensuring that index-based access is very fast.
|
---|
95 |
|
---|
96 | Items can be added at either end of the list using
|
---|
97 | QList::append() and QList::prepend(), or they can be inserted in
|
---|
98 | the middle using QList::insert(). More than any other container
|
---|
99 | class, QList is highly optimized to expand to as little code as
|
---|
100 | possible in the executable. QStringList inherits from
|
---|
101 | QList<QString>.
|
---|
102 |
|
---|
103 | \row \o \l{QLinkedList}<T>
|
---|
104 | \o This is similar to QList, except that it uses
|
---|
105 | iterators rather than integer indexes to access items. It also
|
---|
106 | provides better performance than QList when inserting in the
|
---|
107 | middle of a huge list, and it has nicer iterator semantics.
|
---|
108 | (Iterators pointing to an item in a QLinkedList remain valid as
|
---|
109 | long as the item exists, whereas iterators to a QList can become
|
---|
110 | invalid after any insertion or removal.)
|
---|
111 |
|
---|
112 | \row \o \l{QVector}<T>
|
---|
113 | \o This stores an array of values of a given type at adjacent
|
---|
114 | positions in memory. Inserting at the front or in the middle of
|
---|
115 | a vector can be quite slow, because it can lead to large numbers
|
---|
116 | of items having to be moved by one position in memory.
|
---|
117 |
|
---|
118 | \row \o \l{QStack}<T>
|
---|
119 | \o This is a convenience subclass of QVector that provides
|
---|
120 | "last in, first out" (LIFO) semantics. It adds the following
|
---|
121 | functions to those already present in QVector:
|
---|
122 | \l{QStack::push()}{push()}, \l{QStack::pop()}{pop()},
|
---|
123 | and \l{QStack::top()}{top()}.
|
---|
124 |
|
---|
125 | \row \o \l{QQueue}<T>
|
---|
126 | \o This is a convenience subclass of QList that provides
|
---|
127 | "first in, first out" (FIFO) semantics. It adds the following
|
---|
128 | functions to those already present in QList:
|
---|
129 | \l{QQueue::enqueue()}{enqueue()},
|
---|
130 | \l{QQueue::dequeue()}{dequeue()}, and \l{QQueue::head()}{head()}.
|
---|
131 |
|
---|
132 | \row \o \l{QSet}<T>
|
---|
133 | \o This provides a single-valued mathematical set with fast
|
---|
134 | lookups.
|
---|
135 |
|
---|
136 | \row \o \l{QMap}<Key, T>
|
---|
137 | \o This provides a dictionary (associative array) that maps keys
|
---|
138 | of type Key to values of type T. Normally each key is associated
|
---|
139 | with a single value. QMap stores its data in Key order; if order
|
---|
140 | doesn't matter QHash is a faster alternative.
|
---|
141 |
|
---|
142 | \row \o \l{QMultiMap}<Key, T>
|
---|
143 | \o This is a convenience subclass of QMap that provides a nice
|
---|
144 | interface for multi-valued maps, i.e. maps where one key can be
|
---|
145 | associated with multiple values.
|
---|
146 |
|
---|
147 | \row \o \l{QHash}<Key, T>
|
---|
148 | \o This has almost the same API as QMap, but provides
|
---|
149 | significantly faster lookups. QHash stores its data in an
|
---|
150 | arbitrary order.
|
---|
151 |
|
---|
152 | \row \o \l{QMultiHash}<Key, T>
|
---|
153 | \o This is a convenience subclass of QHash that
|
---|
154 | provides a nice interface for multi-valued hashes.
|
---|
155 |
|
---|
156 | \endtable
|
---|
157 |
|
---|
158 | Containers can be nested. For example, it is perfectly possible
|
---|
159 | to use a QMap<QString, QList<int> >, where the key type is
|
---|
160 | QString and the value type QList<int>. The only pitfall is that
|
---|
161 | you must insert a space between the closing angle brackets (>);
|
---|
162 | otherwise the C++ compiler will misinterpret the two >'s as a
|
---|
163 | right-shift operator (>>) and report a syntax error.
|
---|
164 |
|
---|
165 | The containers are defined in individual header files with the
|
---|
166 | same name as the container (e.g., \c <QLinkedList>). For
|
---|
167 | convenience, the containers are forward declared in \c
|
---|
168 | <QtContainerFwd>.
|
---|
169 |
|
---|
170 | \keyword assignable data type
|
---|
171 | \keyword assignable data types
|
---|
172 |
|
---|
173 | The values stored in the various containers can be of any
|
---|
174 | \e{assignable data type}. To qualify, a type must provide a
|
---|
175 | default constructor, a copy constructor, and an assignment
|
---|
176 | operator. This covers most data types you are likely to want to
|
---|
177 | store in a container, including basic types such as \c int and \c
|
---|
178 | double, pointer types, and Qt data types such as QString, QDate,
|
---|
179 | and QTime, but it doesn't cover QObject or any QObject subclass
|
---|
180 | (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a
|
---|
181 | QList<QWidget>, the compiler will complain that QWidget's copy
|
---|
182 | constructor and assignment operators are disabled. If you want to
|
---|
183 | store these kinds of objects in a container, store them as
|
---|
184 | pointers, for example as QList<QWidget *>.
|
---|
185 |
|
---|
186 | Here's an example custom data type that meets the requirement of
|
---|
187 | an assignable data type:
|
---|
188 |
|
---|
189 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 0
|
---|
190 |
|
---|
191 | If we don't provide a copy constructor or an assignment operator,
|
---|
192 | C++ provides a default implementation that performs a
|
---|
193 | member-by-member copy. In the example above, that would have been
|
---|
194 | sufficient. Also, if you don't provide any constructors, C++
|
---|
195 | provides a default constructor that initializes its member using
|
---|
196 | default constructors. Although it doesn't provide any
|
---|
197 | explicit constructors or assignment operator, the following data
|
---|
198 | type can be stored in a container:
|
---|
199 |
|
---|
200 | \snippet doc/src/snippets/streaming/main.cpp 0
|
---|
201 |
|
---|
202 | Some containers have additional requirements for the data types
|
---|
203 | they can store. For example, the Key type of a QMap<Key, T> must
|
---|
204 | provide \c operator<(). Such special requirements are documented
|
---|
205 | in a class's detailed description. In some cases, specific
|
---|
206 | functions have special requirements; these are described on a
|
---|
207 | per-function basis. The compiler will always emit an error if a
|
---|
208 | requirement isn't met.
|
---|
209 |
|
---|
210 | Qt's containers provide operator<<() and operator>>() so that they
|
---|
211 | can easily be read and written using a QDataStream. This means
|
---|
212 | that the data types stored in the container must also support
|
---|
213 | operator<<() and operator>>(). Providing such support is
|
---|
214 | straightforward; here's how we could do it for the Movie struct
|
---|
215 | above:
|
---|
216 |
|
---|
217 | \snippet doc/src/snippets/streaming/main.cpp 1
|
---|
218 | \codeline
|
---|
219 | \snippet doc/src/snippets/streaming/main.cpp 2
|
---|
220 |
|
---|
221 | \keyword default-constructed values
|
---|
222 |
|
---|
223 | The documentation of certain container class functions refer to
|
---|
224 | \e{default-constructed values}; for example, QVector
|
---|
225 | automatically initializes its items with default-constructed
|
---|
226 | values, and QMap::value() returns a default-constructed value if
|
---|
227 | the specified key isn't in the map. For most value types, this
|
---|
228 | simply means that a value is created using the default
|
---|
229 | constructor (e.g. an empty string for QString). But for primitive
|
---|
230 | types like \c{int} and \c{double}, as well as for pointer types,
|
---|
231 | the C++ language doesn't specify any initialization; in those
|
---|
232 | cases, Qt's containers automatically initialize the value to 0.
|
---|
233 |
|
---|
234 | \section1 The Iterator Classes
|
---|
235 |
|
---|
236 | Iterators provide a uniform means to access items in a container.
|
---|
237 | Qt's container classes provide two types of iterators: Java-style
|
---|
238 | iterators and STL-style iterators.
|
---|
239 |
|
---|
240 | \section2 Java-Style Iterators
|
---|
241 |
|
---|
242 | The Java-style iterators are new in Qt 4 and are the standard
|
---|
243 | ones used in Qt applications. They are more convenient to use than
|
---|
244 | the STL-style iterators, at the price of being slightly less
|
---|
245 | efficient. Their API is modelled on Java's iterator classes.
|
---|
246 |
|
---|
247 | For each container class, there are two Java-style iterator data
|
---|
248 | types: one that provides read-only access and one that provides
|
---|
249 | read-write access.
|
---|
250 |
|
---|
251 | \table
|
---|
252 | \header \o Containers \o Read-only iterator
|
---|
253 | \o Read-write iterator
|
---|
254 | \row \o QList<T>, QQueue<T> \o QListIterator<T>
|
---|
255 | \o QMutableListIterator<T>
|
---|
256 | \row \o QLinkedList<T> \o QLinkedListIterator<T>
|
---|
257 | \o QMutableLinkedListIterator<T>
|
---|
258 | \row \o QVector<T>, QStack<T> \o QVectorIterator<T>
|
---|
259 | \o QMutableVectorIterator<T>
|
---|
260 | \row \o QSet<T> \o QSetIterator<T>
|
---|
261 | \o QMutableSetIterator<T>
|
---|
262 | \row \o QMap<Key, T>, QMultiMap<Key, T> \o QMapIterator<Key, T>
|
---|
263 | \o QMutableMapIterator<Key, T>
|
---|
264 | \row \o QHash<Key, T>, QMultiHash<Key, T> \o QHashIterator<Key, T>
|
---|
265 | \o QMutableHashIterator<Key, T>
|
---|
266 | \endtable
|
---|
267 |
|
---|
268 | In this discussion, we will concentrate on QList and QMap. The
|
---|
269 | iterator types for QLinkedList, QVector, and QSet have exactly
|
---|
270 | the same interface as QList's iterators; similarly, the iterator
|
---|
271 | types for QHash have the same interface as QMap's iterators.
|
---|
272 |
|
---|
273 | Unlike STL-style iterators (covered \l{STL-style
|
---|
274 | iterators}{below}), Java-style iterators point \e between items
|
---|
275 | rather than directly \e at items. For this reason, they are
|
---|
276 | either pointing to the very beginning of the container (before
|
---|
277 | the first item), at the very end of the container (after the last
|
---|
278 | item), or between two items. The diagram below shows the valid
|
---|
279 | iterator positions as red arrows for a list containing four
|
---|
280 | items:
|
---|
281 |
|
---|
282 | \img javaiterators1.png
|
---|
283 |
|
---|
284 | Here's a typical loop for iterating through all the elements of a
|
---|
285 | QList<QString> in order and printing them to the console:
|
---|
286 |
|
---|
287 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 1
|
---|
288 |
|
---|
289 | It works as follows: The QList to iterate over is passed to the
|
---|
290 | QListIterator constructor. At that point, the iterator is located
|
---|
291 | just in front of the first item in the list (before item "A").
|
---|
292 | Then we call \l{QListIterator::hasNext()}{hasNext()} to
|
---|
293 | check whether there is an item after the iterator. If there is, we
|
---|
294 | call \l{QListIterator::next()}{next()} to jump over that
|
---|
295 | item. The next() function returns the item that it jumps over. For
|
---|
296 | a QList<QString>, that item is of type QString.
|
---|
297 |
|
---|
298 | Here's how to iterate backward in a QList:
|
---|
299 |
|
---|
300 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 2
|
---|
301 |
|
---|
302 | The code is symmetric with iterating forward, except that we
|
---|
303 | start by calling \l{QListIterator::toBack()}{toBack()}
|
---|
304 | to move the iterator after the last item in the list.
|
---|
305 |
|
---|
306 | The diagram below illustrates the effect of calling
|
---|
307 | \l{QListIterator::next()}{next()} and
|
---|
308 | \l{QListIterator::previous()}{previous()} on an iterator:
|
---|
309 |
|
---|
310 | \img javaiterators2.png
|
---|
311 |
|
---|
312 | The following table summarizes the QListIterator API:
|
---|
313 |
|
---|
314 | \table
|
---|
315 | \header \o Function \o Behavior
|
---|
316 | \row \o \l{QListIterator::toFront()}{toFront()}
|
---|
317 | \o Moves the iterator to the front of the list (before the first item)
|
---|
318 | \row \o \l{QListIterator::toBack()}{toBack()}
|
---|
319 | \o Moves the iterator to the back of the list (after the last item)
|
---|
320 | \row \o \l{QListIterator::hasNext()}{hasNext()}
|
---|
321 | \o Returns true if the iterator isn't at the back of the list
|
---|
322 | \row \o \l{QListIterator::next()}{next()}
|
---|
323 | \o Returns the next item and advances the iterator by one position
|
---|
324 | \row \o \l{QListIterator::peekNext()}{peekNext()}
|
---|
325 | \o Returns the next item without moving the iterator
|
---|
326 | \row \o \l{QListIterator::hasPrevious()}{hasPrevious()}
|
---|
327 | \o Returns true if the iterator isn't at the front of the list
|
---|
328 | \row \o \l{QListIterator::previous()}{previous()}
|
---|
329 | \o Returns the previous item and moves the iterator back by one position
|
---|
330 | \row \o \l{QListIterator::peekPrevious()}{peekPrevious()}
|
---|
331 | \o Returns the previous item without moving the iterator
|
---|
332 | \endtable
|
---|
333 |
|
---|
334 | QListIterator provides no functions to insert or remove items
|
---|
335 | from the list as we iterate. To accomplish this, you must use
|
---|
336 | QMutableListIterator. Here's an example where we remove all
|
---|
337 | odd numbers from a QList<int> using QMutableListIterator:
|
---|
338 |
|
---|
339 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 3
|
---|
340 |
|
---|
341 | The next() call in the loop is made every time. It jumps over the
|
---|
342 | next item in the list. The
|
---|
343 | \l{QMutableListIterator::remove()}{remove()} function removes the
|
---|
344 | last item that we jumped over from the list. The call to
|
---|
345 | \l{QMutableListIterator::remove()}{remove()} does not invalidate
|
---|
346 | the iterator, so it is safe to continue using it. This works just
|
---|
347 | as well when iterating backward:
|
---|
348 |
|
---|
349 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 4
|
---|
350 |
|
---|
351 | If we just want to modify the value of an existing item, we can
|
---|
352 | use \l{QMutableListIterator::setValue()}{setValue()}. In the code
|
---|
353 | below, we replace any value larger than 128 with 128:
|
---|
354 |
|
---|
355 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 5
|
---|
356 |
|
---|
357 | Just like \l{QMutableListIterator::remove()}{remove()},
|
---|
358 | \l{QMutableListIterator::setValue()}{setValue()} operates on the
|
---|
359 | last item that we jumped over. If we iterate forward, this is the
|
---|
360 | item just before the iterator; if we iterate backward, this is
|
---|
361 | the item just after the iterator.
|
---|
362 |
|
---|
363 | The \l{QMutableListIterator::next()}{next()} function returns a
|
---|
364 | non-const reference to the item in the list. For simple
|
---|
365 | operations, we don't even need
|
---|
366 | \l{QMutableListIterator::setValue()}{setValue()}:
|
---|
367 |
|
---|
368 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 6
|
---|
369 |
|
---|
370 | As mentioned above, QLinkedList's, QVector's, and QSet's iterator
|
---|
371 | classes have exactly the same API as QList's. We will now turn to
|
---|
372 | QMapIterator, which is somewhat different because it iterates on
|
---|
373 | (key, value) pairs.
|
---|
374 |
|
---|
375 | Like QListIterator, QMapIterator provides
|
---|
376 | \l{QMapIterator::toFront()}{toFront()},
|
---|
377 | \l{QMapIterator::toBack()}{toBack()},
|
---|
378 | \l{QMapIterator::hasNext()}{hasNext()},
|
---|
379 | \l{QMapIterator::next()}{next()},
|
---|
380 | \l{QMapIterator::peekNext()}{peekNext()},
|
---|
381 | \l{QMapIterator::hasPrevious()}{hasPrevious()},
|
---|
382 | \l{QMapIterator::previous()}{previous()}, and
|
---|
383 | \l{QMapIterator::peekPrevious()}{peekPrevious()}. The key and
|
---|
384 | value components are extracted by calling key() and value() on
|
---|
385 | the object returned by next(), peekNext(), previous(), or
|
---|
386 | peekPrevious().
|
---|
387 |
|
---|
388 | The following example removes all (capital, country) pairs where
|
---|
389 | the capital's name ends with "City":
|
---|
390 |
|
---|
391 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 7
|
---|
392 |
|
---|
393 | QMapIterator also provides a key() and a value() function that
|
---|
394 | operate directly on the iterator and that return the key and
|
---|
395 | value of the last item that the iterator jumped above. For
|
---|
396 | example, the following code copies the contents of a QMap into a
|
---|
397 | QHash:
|
---|
398 |
|
---|
399 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 8
|
---|
400 |
|
---|
401 | If we want to iterate through all the items with the same
|
---|
402 | value, we can use \l{QMapIterator::findNext()}{findNext()}
|
---|
403 | or \l{QMapIterator::findPrevious()}{findPrevious()}.
|
---|
404 | Here's an example where we remove all the items with a particular
|
---|
405 | value:
|
---|
406 |
|
---|
407 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 9
|
---|
408 |
|
---|
409 | \section2 STL-Style Iterators
|
---|
410 |
|
---|
411 | STL-style iterators have been available since the release of Qt
|
---|
412 | 2.0. They are compatible with Qt's and STL's \l{generic
|
---|
413 | algorithms} and are optimized for speed.
|
---|
414 |
|
---|
415 | For each container class, there are two STL-style iterator types:
|
---|
416 | one that provides read-only access and one that provides
|
---|
417 | read-write access. Read-only iterators should be used wherever
|
---|
418 | possible because they are faster than read-write iterators.
|
---|
419 |
|
---|
420 | \table
|
---|
421 | \header \o Containers \o Read-only iterator
|
---|
422 | \o Read-write iterator
|
---|
423 | \row \o QList<T>, QQueue<T> \o QList<T>::const_iterator
|
---|
424 | \o QList<T>::iterator
|
---|
425 | \row \o QLinkedList<T> \o QLinkedList<T>::const_iterator
|
---|
426 | \o QLinkedList<T>::iterator
|
---|
427 | \row \o QVector<T>, QStack<T> \o QVector<T>::const_iterator
|
---|
428 | \o QVector<T>::iterator
|
---|
429 | \row \o QSet<T> \o QSet<T>::const_iterator
|
---|
430 | \o QSet<T>::iterator
|
---|
431 | \row \o QMap<Key, T>, QMultiMap<Key, T> \o QMap<Key, T>::const_iterator
|
---|
432 | \o QMap<Key, T>::iterator
|
---|
433 | \row \o QHash<Key, T>, QMultiHash<Key, T> \o QHash<Key, T>::const_iterator
|
---|
434 | \o QHash<Key, T>::iterator
|
---|
435 | \endtable
|
---|
436 |
|
---|
437 | The API of the STL iterators is modelled on pointers in an array.
|
---|
438 | For example, the \c ++ operator advances the iterator to the next
|
---|
439 | item, and the \c * operator returns the item that the iterator
|
---|
440 | points to. In fact, for QVector and QStack, which store their
|
---|
441 | items at adjacent memory positions, the
|
---|
442 | \l{QVector::iterator}{iterator} type is just a typedef for \c{T *},
|
---|
443 | and the \l{QVector::iterator}{const_iterator} type is
|
---|
444 | just a typedef for \c{const T *}.
|
---|
445 |
|
---|
446 | In this discussion, we will concentrate on QList and QMap. The
|
---|
447 | iterator types for QLinkedList, QVector, and QSet have exactly
|
---|
448 | the same interface as QList's iterators; similarly, the iterator
|
---|
449 | types for QHash have the same interface as QMap's iterators.
|
---|
450 |
|
---|
451 | Here's a typical loop for iterating through all the elements of a
|
---|
452 | QList<QString> in order and converting them to lowercase:
|
---|
453 |
|
---|
454 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 10
|
---|
455 |
|
---|
456 | Unlike \l{Java-style iterators}, STL-style iterators point
|
---|
457 | directly at items. The begin() function of a container returns an
|
---|
458 | iterator that points to the first item in the container. The
|
---|
459 | end() function of a container returns an iterator to the
|
---|
460 | imaginary item one position past the last item in the container.
|
---|
461 | end() marks an invalid position; it must never be dereferenced.
|
---|
462 | It is typically used in a loop's break condition. If the list is
|
---|
463 | empty, begin() equals end(), so we never execute the loop.
|
---|
464 |
|
---|
465 | The diagram below shows the valid iterator positions as red
|
---|
466 | arrows for a vector containing four items:
|
---|
467 |
|
---|
468 | \img stliterators1.png
|
---|
469 |
|
---|
470 | Iterating backward with an STL-style iterator requires us to
|
---|
471 | decrement the iterator \e before we access the item. This
|
---|
472 | requires a \c while loop:
|
---|
473 |
|
---|
474 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 11
|
---|
475 |
|
---|
476 | In the code snippets so far, we used the unary \c * operator to
|
---|
477 | retrieve the item (of type QString) stored at a certain iterator
|
---|
478 | position, and we then called QString::toLower() on it. Most C++
|
---|
479 | compilers also allow us to write \c{i->toLower()}, but some
|
---|
480 | don't.
|
---|
481 |
|
---|
482 | For read-only access, you can use const_iterator, constBegin(),
|
---|
483 | and constEnd(). For example:
|
---|
484 |
|
---|
485 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 12
|
---|
486 |
|
---|
487 | The following table summarizes the STL-style iterators' API:
|
---|
488 |
|
---|
489 | \table
|
---|
490 | \header \o Expression \o Behavior
|
---|
491 | \row \o \c{*i} \o Returns the current item
|
---|
492 | \row \o \c{++i} \o Advances the iterator to the next item
|
---|
493 | \row \o \c{i += n} \o Advances the iterator by \c n items
|
---|
494 | \row \o \c{--i} \o Moves the iterator back by one item
|
---|
495 | \row \o \c{i -= n} \o Moves the iterator back by \c n items
|
---|
496 | \row \o \c{i - j} \o Returns the number of items between iterators \c i and \c j
|
---|
497 | \endtable
|
---|
498 |
|
---|
499 | The \c{++} and \c{--} operators are available both as prefix
|
---|
500 | (\c{++i}, \c{--i}) and postfix (\c{i++}, \c{i--}) operators. The
|
---|
501 | prefix versions modify the iterators and return a reference to
|
---|
502 | the modified iterator; the postfix versions take a copy of the
|
---|
503 | iterator before they modify it, and return that copy. In
|
---|
504 | expressions where the return value is ignored, we recommend that
|
---|
505 | you use the prefix operators (\c{++i}, \c{--i}), as these are
|
---|
506 | slightly faster.
|
---|
507 |
|
---|
508 | For non-const iterator types, the return value of the unary \c{*}
|
---|
509 | operator can be used on the left side of the assignment operator.
|
---|
510 |
|
---|
511 | For QMap and QHash, the \c{*} operator returns the value
|
---|
512 | component of an item. If you want to retrieve the key, call key()
|
---|
513 | on the iterator. For symmetry, the iterator types also provide a
|
---|
514 | value() function to retrieve the value. For example, here's how
|
---|
515 | we would print all items in a QMap to the console:
|
---|
516 |
|
---|
517 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 13
|
---|
518 |
|
---|
519 | Thanks to \l{implicit sharing}, it is very inexpensive for a
|
---|
520 | function to return a container per value. The Qt API contains
|
---|
521 | dozens of functions that return a QList or QStringList per value
|
---|
522 | (e.g., QSplitter::sizes()). If you want to iterate over these
|
---|
523 | using an STL iterator, you should always take a copy of the
|
---|
524 | container and iterate over the copy. For example:
|
---|
525 |
|
---|
526 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 14
|
---|
527 |
|
---|
528 | This problem doesn't occur with functions that return a const or
|
---|
529 | non-const reference to a container.
|
---|
530 |
|
---|
531 | \l{Implicit sharing} has another consequence on STL-style
|
---|
532 | iterators: You must not take a copy of a container while
|
---|
533 | non-const iterators are active on that container. Java-style
|
---|
534 | iterators don't suffer from that limitation.
|
---|
535 |
|
---|
536 | \keyword foreach
|
---|
537 | \section1 The foreach Keyword
|
---|
538 |
|
---|
539 | If you just want to iterate over all the items in a container
|
---|
540 | in order, you can use Qt's \c foreach keyword. The keyword is a
|
---|
541 | Qt-specific addition to the C++ language, and is implemented
|
---|
542 | using the preprocessor.
|
---|
543 |
|
---|
544 | Its syntax is: \c foreach (\e variable, \e container) \e
|
---|
545 | statement. For example, here's how to use \c foreach to iterate
|
---|
546 | over a QLinkedList<QString>:
|
---|
547 |
|
---|
548 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 15
|
---|
549 |
|
---|
550 | The \c foreach code is significantly shorter than the equivalent
|
---|
551 | code that uses iterators:
|
---|
552 |
|
---|
553 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 16
|
---|
554 |
|
---|
555 | Unless the data type contains a comma (e.g., \c{QPair<int,
|
---|
556 | int>}), the variable used for iteration can be defined within the
|
---|
557 | \c foreach statement:
|
---|
558 |
|
---|
559 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 17
|
---|
560 |
|
---|
561 | And like any other C++ loop construct, you can use braces around
|
---|
562 | the body of a \c foreach loop, and you can use \c break to leave
|
---|
563 | the loop:
|
---|
564 |
|
---|
565 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 18
|
---|
566 |
|
---|
567 | With QMap and QHash, \c foreach accesses the value component of
|
---|
568 | the (key, value) pairs. If you want to iterate over both the keys
|
---|
569 | and the values, you can use iterators (which are fastest), or you
|
---|
570 | can write code like this:
|
---|
571 |
|
---|
572 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 19
|
---|
573 |
|
---|
574 | For a multi-valued map:
|
---|
575 |
|
---|
576 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 20
|
---|
577 |
|
---|
578 | Qt automatically takes a copy of the container when it enters a
|
---|
579 | \c foreach loop. If you modify the container as you are
|
---|
580 | iterating, that won't affect the loop. (If you don't modify the
|
---|
581 | container, the copy still takes place, but thanks to \l{implicit
|
---|
582 | sharing} copying a container is very fast.) Similarly, declaring
|
---|
583 | the variable to be a non-const reference, in order to modify the
|
---|
584 | current item in the list will not work either.
|
---|
585 |
|
---|
586 | In addition to \c foreach, Qt also provides a \c forever
|
---|
587 | pseudo-keyword for infinite loops:
|
---|
588 |
|
---|
589 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 21
|
---|
590 |
|
---|
591 | If you're worried about namespace pollution, you can disable
|
---|
592 | these macros by adding the following line to your \c .pro file:
|
---|
593 |
|
---|
594 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 22
|
---|
595 |
|
---|
596 | \section1 Other Container-Like Classes
|
---|
597 |
|
---|
598 | Qt includes three template classes that resemble containers in
|
---|
599 | some respects. These classes don't provide iterators and cannot
|
---|
600 | be used with the \c foreach keyword.
|
---|
601 |
|
---|
602 | \list
|
---|
603 | \o QVarLengthArray<T, Prealloc> provides a low-level
|
---|
604 | variable-length array. It can be used instead of QVector in
|
---|
605 | places where speed is particularly important.
|
---|
606 |
|
---|
607 | \o QCache<Key, T> provides a cache to store objects of a certain
|
---|
608 | type T associated with keys of type Key.
|
---|
609 |
|
---|
610 | \o QPair<T1, T2> stores a pair of elements.
|
---|
611 | \endlist
|
---|
612 |
|
---|
613 | Additional non-template types that compete with Qt's template
|
---|
614 | containers are QBitArray, QByteArray, QString, and QStringList.
|
---|
615 |
|
---|
616 | \section1 Algorithmic Complexity
|
---|
617 |
|
---|
618 | Algorithmic complexity is concerned about how fast (or slow) each
|
---|
619 | function is as the number of items in the container grow. For
|
---|
620 | example, inserting an item in the middle of a QLinkedList is an
|
---|
621 | extremely fast operation, irrespective of the number of items
|
---|
622 | stored in the QLinkedList. On the other hand, inserting an item
|
---|
623 | in the middle of a QVector is potentially very expensive if the
|
---|
624 | QVector contains many items, since half of the items must be
|
---|
625 | moved one position in memory.
|
---|
626 |
|
---|
627 | To describe algorithmic complexity, we use the following
|
---|
628 | terminology, based on the "big Oh" notation:
|
---|
629 |
|
---|
630 | \keyword constant time
|
---|
631 | \keyword logarithmic time
|
---|
632 | \keyword linear time
|
---|
633 | \keyword linear-logarithmic time
|
---|
634 | \keyword quadratic time
|
---|
635 |
|
---|
636 | \list
|
---|
637 | \o \bold{Constant time:} O(1). A function is said to run in constant
|
---|
638 | time if it requires the same amount of time no matter how many
|
---|
639 | items are present in the container. One example is
|
---|
640 | QLinkedList::insert().
|
---|
641 |
|
---|
642 | \o \bold{Logarithmic time:} O(log \e n). A function that runs in
|
---|
643 | logarithmic time is a function whose running time is
|
---|
644 | proportional to the logarithm of the number of items in the
|
---|
645 | container. One example is qBinaryFind().
|
---|
646 |
|
---|
647 | \o \bold{Linear time:} O(\e n). A function that runs in linear time
|
---|
648 | will execute in a time directly proportional to the number of
|
---|
649 | items stored in the container. One example is
|
---|
650 | QVector::insert().
|
---|
651 |
|
---|
652 | \o \bold{Linear-logarithmic time:} O(\e{n} log \e n). A function
|
---|
653 | that runs in linear-logarithmic time is asymptotically slower
|
---|
654 | than a linear-time function, but faster than a quadratic-time
|
---|
655 | function.
|
---|
656 |
|
---|
657 | \o \bold{Quadratic time:} O(\e{n}\unicode{178}). A quadratic-time function
|
---|
658 | executes in a time that is proportional to the square of the
|
---|
659 | number of items stored in the container.
|
---|
660 | \endlist
|
---|
661 |
|
---|
662 | The following table summarizes the algorithmic complexity of Qt's
|
---|
663 | sequential container classes:
|
---|
664 |
|
---|
665 | \table
|
---|
666 | \header \o \o Index lookup \o Insertion \o Prepending \o Appending
|
---|
667 | \row \o QLinkedList<T> \o O(\e n) \o O(1) \o O(1) \o O(1)
|
---|
668 | \row \o QList<T> \o O(1) \o O(n) \o Amort. O(1) \o Amort. O(1)
|
---|
669 | \row \o QVector<T> \o O(1) \o O(n) \o O(n) \o Amort. O(1)
|
---|
670 | \endtable
|
---|
671 |
|
---|
672 | In the table, "Amort." stands for "amortized behavior". For
|
---|
673 | example, "Amort. O(1)" means that if you call the function
|
---|
674 | only once, you might get O(\e n) behavior, but if you call it
|
---|
675 | multiple times (e.g., \e n times), the average behavior will be
|
---|
676 | O(1).
|
---|
677 |
|
---|
678 | The following table summarizes the algorithmic complexity of Qt's
|
---|
679 | associative containers and sets:
|
---|
680 |
|
---|
681 | \table
|
---|
682 | \header \o{1,2} \o{2,1} Key lookup \o{2,1} Insertion
|
---|
683 | \header \o Average \o Worst case \o Average \o Worst case
|
---|
684 | \row \o QMap<Key, T> \o O(log \e n) \o O(log \e n) \o O(log \e n) \o O(log \e n)
|
---|
685 | \row \o QMultiMap<Key, T> \o O((log \e n) \o O(log \e n) \o O(log \e n) \o O(log \e n)
|
---|
686 | \row \o QHash<Key, T> \o Amort. O(1) \o O(\e n) \o Amort. O(1) \o O(\e n)
|
---|
687 | \row \o QSet<Key> \o Amort. O(1) \o O(\e n) \o Amort. O(1) \o O(\e n)
|
---|
688 | \endtable
|
---|
689 |
|
---|
690 | With QVector, QHash, and QSet, the performance of appending items
|
---|
691 | is amortized O(log \e n). It can be brought down to O(1) by
|
---|
692 | calling QVector::reserve(), QHash::reserve(), or QSet::reserve()
|
---|
693 | with the expected number of items before you insert the items.
|
---|
694 | The next section discusses this topic in more depth.
|
---|
695 |
|
---|
696 | \section1 Growth Strategies
|
---|
697 |
|
---|
698 | QVector<T>, QString, and QByteArray store their items
|
---|
699 | contiguously in memory; QList<T> maintains an array of pointers
|
---|
700 | to the items it stores to provide fast index-based access (unless
|
---|
701 | T is a pointer type or a basic type of the size of a pointer, in
|
---|
702 | which case the value itself is stored in the array); QHash<Key,
|
---|
703 | T> keeps a hash table whose size is proportional to the number
|
---|
704 | of items in the hash. To avoid reallocating the data every single
|
---|
705 | time an item is added at the end of the container, these classes
|
---|
706 | typically allocate more memory than necessary.
|
---|
707 |
|
---|
708 | Consider the following code, which builds a QString from another
|
---|
709 | QString:
|
---|
710 |
|
---|
711 | \snippet doc/src/snippets/code/doc_src_containers.qdoc 23
|
---|
712 |
|
---|
713 | We build the string \c out dynamically by appending one character
|
---|
714 | to it at a time. Let's assume that we append 15000 characters to
|
---|
715 | the QString string. Then the following 18 reallocations (out of a
|
---|
716 | possible 15000) occur when QString runs out of space: 4, 8, 12,
|
---|
717 | 16, 20, 52, 116, 244, 500, 1012, 2036, 4084, 6132, 8180, 10228,
|
---|
718 | 12276, 14324, 16372. At the end, the QString has 16372 Unicode
|
---|
719 | characters allocated, 15000 of which are occupied.
|
---|
720 |
|
---|
721 | The values above may seem a bit strange, but here are the guiding
|
---|
722 | principles:
|
---|
723 | \list
|
---|
724 | \o QString allocates 4 characters at a time until it reaches size 20.
|
---|
725 | \o From 20 to 4084, it advances by doubling the size each time.
|
---|
726 | More precisely, it advances to the next power of two, minus
|
---|
727 | 12. (Some memory allocators perform worst when requested exact
|
---|
728 | powers of two, because they use a few bytes per block for
|
---|
729 | book-keeping.)
|
---|
730 | \o From 4084 on, it advances by blocks of 2048 characters (4096
|
---|
731 | bytes). This makes sense because modern operating systems
|
---|
732 | don't copy the entire data when reallocating a buffer; the
|
---|
733 | physical memory pages are simply reordered, and only the data
|
---|
734 | on the first and last pages actually needs to be copied.
|
---|
735 | \endlist
|
---|
736 |
|
---|
737 | QByteArray and QList<T> use more or less the same algorithm as
|
---|
738 | QString.
|
---|
739 |
|
---|
740 | QVector<T> also uses that algorithm for data types that can be
|
---|
741 | moved around in memory using memcpy() (including the basic C++
|
---|
742 | types, the pointer types, and Qt's \l{shared classes}) but uses a
|
---|
743 | different algorithm for data types that can only be moved by
|
---|
744 | calling the copy constructor and a destructor. Since the cost of
|
---|
745 | reallocating is higher in that case, QVector<T> reduces the
|
---|
746 | number of reallocations by always doubling the memory when
|
---|
747 | running out of space.
|
---|
748 |
|
---|
749 | QHash<Key, T> is a totally different case. QHash's internal hash
|
---|
750 | table grows by powers of two, and each time it grows, the items
|
---|
751 | are relocated in a new bucket, computed as qHash(\e key) %
|
---|
752 | QHash::capacity() (the number of buckets). This remark applies to
|
---|
753 | QSet<T> and QCache<Key, T> as well.
|
---|
754 |
|
---|
755 | For most applications, the default growing algorithm provided by
|
---|
756 | Qt does the trick. If you need more control, QVector<T>,
|
---|
757 | QHash<Key, T>, QSet<T>, QString, and QByteArray provide a trio of
|
---|
758 | functions that allow you to check and specify how much memory to
|
---|
759 | use to store the items:
|
---|
760 |
|
---|
761 | \list
|
---|
762 | \o \l{QString::capacity()}{capacity()} returns the
|
---|
763 | number of items for which memory is allocated (for QHash and
|
---|
764 | QSet, the number of buckets in the hash table).
|
---|
765 | \o \l{QString::reserve()}{reserve}(\e size) explicitly
|
---|
766 | preallocates memory for \e size items.
|
---|
767 | \o \l{QString::squeeze()}{squeeze()} frees any memory
|
---|
768 | not required to store the items.
|
---|
769 | \endlist
|
---|
770 |
|
---|
771 | If you know approximately how many items you will store in a
|
---|
772 | container, you can start by calling reserve(), and when you are
|
---|
773 | done populating the container, you can call squeeze() to release
|
---|
774 | the extra preallocated memory.
|
---|
775 | */
|
---|