source: trunk/doc/src/frameworks-technologies/containers.qdoc@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 35.5 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the documentation of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:FDL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \group tools
30 \title Non-GUI Classes
31 \ingroup groups
32
33 \brief Collection classes such as list, queue, stack and string, along
34 with other classes that can be used without needing QApplication.
35
36 The non-GUI classes are general-purpose collection and string classes
37 that may be used independently of the GUI classes.
38
39 In particular, these classes do not depend on QApplication at all,
40 and so can be used in non-GUI programs.
41
42*/
43
44/*!
45 \page containers.html
46 \title Container Classes
47 \ingroup technology-apis
48 \ingroup groups
49 \ingroup qt-basic-concepts
50 \keyword container class
51 \keyword container classes
52
53 \brief Qt's template-based container classes.
54
55 \tableofcontents
56
57 \section1 Introduction
58
59 The Qt library provides a set of general purpose template-based
60 container classes. These classes can be used to store items of a
61 specified type. For example, if you need a resizable array of
62 \l{QString}s, use QVector<QString>.
63
64 These container classes are designed to be lighter, safer, and
65 easier to use than the STL containers. If you are unfamiliar with
66 the STL, or prefer to do things the "Qt way", you can use these
67 classes instead of the STL classes.
68
69 The container classes are \l{implicitly shared}, they are
70 \l{reentrant}, and they are optimized for speed, low memory
71 consumption, and minimal inline code expansion, resulting in
72 smaller executables. In addition, they are \l{thread-safe}
73 in situations where they are used as read-only containers
74 by all threads used to access them.
75
76 For traversing the items stored in a container, you can use one
77 of two types of iterators: \l{Java-style iterators} and
78 \l{STL-style iterators}. The Java-style iterators are easier to
79 use and provide high-level functionality, whereas the STL-style
80 iterators are slightly more efficient and can be used together
81 with Qt's and STL's \l{generic algorithms}.
82
83 Qt also offers a \l{foreach} keyword that make it very
84 easy to iterate over all the items stored in a container.
85
86 \section1 The Container Classes
87
88 Qt provides the following sequential containers: QList,
89 QLinkedList, QVector, QStack, and QQueue. For most
90 applications, QList is the best type to use. Although it is
91 implemented as an array-list, it provides very fast prepends and
92 appends. If you really need a linked-list, use QLinkedList; if you
93 want your items to occupy consecutive memory locations, use QVector.
94 QStack and QQueue are convenience classes that provide LIFO and
95 FIFO semantics.
96
97 Qt also provides these associative containers: QMap,
98 QMultiMap, QHash, QMultiHash, and QSet. The "Multi" containers
99 conveniently support multiple values associated with a single
100 key. The "Hash" containers provide faster lookup by using a hash
101 function instead of a binary search on a sorted set.
102
103 As special cases, the QCache and QContiguousCache classes provide
104 efficient hash-lookup of objects in a limited cache storage.
105
106 \table
107 \header \o Class \o Summary
108
109 \row \o \l{QList}<T>
110 \o This is by far the most commonly used container class. It
111 stores a list of values of a given type (T) that can be accessed
112 by index. Internally, the QList is implemented using an array,
113 ensuring that index-based access is very fast.
114
115 Items can be added at either end of the list using
116 QList::append() and QList::prepend(), or they can be inserted in
117 the middle using QList::insert(). More than any other container
118 class, QList is highly optimized to expand to as little code as
119 possible in the executable. QStringList inherits from
120 QList<QString>.
121
122 \row \o \l{QLinkedList}<T>
123 \o This is similar to QList, except that it uses
124 iterators rather than integer indexes to access items. It also
125 provides better performance than QList when inserting in the
126 middle of a huge list, and it has nicer iterator semantics.
127 (Iterators pointing to an item in a QLinkedList remain valid as
128 long as the item exists, whereas iterators to a QList can become
129 invalid after any insertion or removal.)
130
131 \row \o \l{QVector}<T>
132 \o This stores an array of values of a given type at adjacent
133 positions in memory. Inserting at the front or in the middle of
134 a vector can be quite slow, because it can lead to large numbers
135 of items having to be moved by one position in memory.
136
137 \row \o \l{QStack}<T>
138 \o This is a convenience subclass of QVector that provides
139 "last in, first out" (LIFO) semantics. It adds the following
140 functions to those already present in QVector:
141 \l{QStack::push()}{push()}, \l{QStack::pop()}{pop()},
142 and \l{QStack::top()}{top()}.
143
144 \row \o \l{QQueue}<T>
145 \o This is a convenience subclass of QList that provides
146 "first in, first out" (FIFO) semantics. It adds the following
147 functions to those already present in QList:
148 \l{QQueue::enqueue()}{enqueue()},
149 \l{QQueue::dequeue()}{dequeue()}, and \l{QQueue::head()}{head()}.
150
151 \row \o \l{QSet}<T>
152 \o This provides a single-valued mathematical set with fast
153 lookups.
154
155 \row \o \l{QMap}<Key, T>
156 \o This provides a dictionary (associative array) that maps keys
157 of type Key to values of type T. Normally each key is associated
158 with a single value. QMap stores its data in Key order; if order
159 doesn't matter QHash is a faster alternative.
160
161 \row \o \l{QMultiMap}<Key, T>
162 \o This is a convenience subclass of QMap that provides a nice
163 interface for multi-valued maps, i.e. maps where one key can be
164 associated with multiple values.
165
166 \row \o \l{QHash}<Key, T>
167 \o This has almost the same API as QMap, but provides
168 significantly faster lookups. QHash stores its data in an
169 arbitrary order.
170
171 \row \o \l{QMultiHash}<Key, T>
172 \o This is a convenience subclass of QHash that
173 provides a nice interface for multi-valued hashes.
174
175 \endtable
176
177 Containers can be nested. For example, it is perfectly possible
178 to use a QMap<QString, QList<int> >, where the key type is
179 QString and the value type QList<int>. The only pitfall is that
180 you must insert a space between the closing angle brackets (>);
181 otherwise the C++ compiler will misinterpret the two >'s as a
182 right-shift operator (>>) and report a syntax error.
183
184 The containers are defined in individual header files with the
185 same name as the container (e.g., \c <QLinkedList>). For
186 convenience, the containers are forward declared in \c
187 <QtContainerFwd>.
188
189 \keyword assignable data type
190 \keyword assignable data types
191
192 The values stored in the various containers can be of any
193 \e{assignable data type}. To qualify, a type must provide a
194 default constructor, a copy constructor, and an assignment
195 operator. This covers most data types you are likely to want to
196 store in a container, including basic types such as \c int and \c
197 double, pointer types, and Qt data types such as QString, QDate,
198 and QTime, but it doesn't cover QObject or any QObject subclass
199 (QWidget, QDialog, QTimer, etc.). If you attempt to instantiate a
200 QList<QWidget>, the compiler will complain that QWidget's copy
201 constructor and assignment operators are disabled. If you want to
202 store these kinds of objects in a container, store them as
203 pointers, for example as QList<QWidget *>.
204
205 Here's an example custom data type that meets the requirement of
206 an assignable data type:
207
208 \snippet doc/src/snippets/code/doc_src_containers.qdoc 0
209
210 If we don't provide a copy constructor or an assignment operator,
211 C++ provides a default implementation that performs a
212 member-by-member copy. In the example above, that would have been
213 sufficient. Also, if you don't provide any constructors, C++
214 provides a default constructor that initializes its member using
215 default constructors. Although it doesn't provide any