[556] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2009 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:LGPL$
|
---|
| 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
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 16 | ** GNU Lesser General Public License Usage
|
---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
| 18 | ** General Public License version 2.1 as published by the Free Software
|
---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
| 20 | ** packaging of this file. Please review the following information to
|
---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
| 23 | **
|
---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
| 25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | /*!
|
---|
| 43 | \group tools
|
---|
| 44 | \title Non-GUI Classes
|
---|
| 45 | \ingroup groups
|
---|
| 46 |
|
---|
| 47 | \brief Collection classes such as list, queue, stack and string, along
|
---|
| 48 | with other classes that can be used without needing QApplication.
|
---|
| 49 |
|
---|
| 50 | The non-GUI classes are general-purpose collection and string classes
|
---|
| 51 | that may be used independently of the GUI classes.
|
---|
| 52 |
|
---|
| 53 | In particular, these classes do not depend on QApplication at all,
|
---|
| 54 | and so can be used in non-GUI programs.
|
---|
| 55 |
|
---|
| 56 | */
|
---|
| 57 |
|
---|
| 58 | /*!
|
---|
| 59 | \page containers.html
|
---|
| 60 | \title Generic Containers
|
---|
| 61 | \ingroup frameworks-technologies
|
---|
| 62 | \ingroup groups
|
---|
| 63 | \keyword container class
|
---|
| 64 | \keyword container classes
|
---|
| 65 |
|
---|
| 66 | \brief Qt's template-based container classes.
|
---|
| 67 |
|
---|
| 68 | \tableofcontents
|
---|
| 69 |
|
---|
| 70 | \section1 Introduction
|
---|
| 71 |
|
---|
| 72 | The Qt library provides a set of general purpose template-based
|
---|
| 73 | container classes. These classes can be used to store items of a
|
---|
| 74 | specified type. For example, if you need a resizable array of
|
---|
| 75 | \l{QString}s, use QVector<QString>.
|
---|
| 76 |
|
---|
| 77 | These container classes are designed to be lighter, safer, and
|
---|
| 78 | easier to use than the STL containers. If you are unfamiliar with
|
---|
| 79 | the STL, or prefer to do things the "Qt way", you can use these
|
---|
| 80 | classes instead of the STL classes.
|
---|
| 81 |
|
---|
| 82 | The container classes are \l{implicitly shared}, they are
|
---|
| 83 | \l{reentrant}, and they are optimized for speed, low memory
|
---|
| 84 | consumption, and minimal inline code expansion, resulting in
|
---|
| 85 | smaller executables. In addition, they are \l{thread-safe}
|
---|
| 86 | in situations where they are used as read-only containers
|
---|
| 87 | by all threads used to access them.
|
---|
| 88 |
|
---|
| 89 | For traversing the items stored in a container, you can use one
|
---|
| 90 | of two types of iterators: \l{Java-style iterators} and
|
---|
| 91 | \l{STL-style iterators}. The Java-style iterators are easier to
|
---|
| 92 | use and provide high-level functionality, whereas the STL-style
|
---|
| 93 | iterators are slightly more efficient and can be used together
|
---|
| 94 | with Qt's and STL's \l{generic algorithms}.
|
---|
| 95 |
|
---|
| 96 | Qt also offers a \l{foreach} keyword that make it very
|
---|
| 97 | easy to iterate over all the items stored in a container.
|
---|
| 98 |
|
---|
| 99 | \section1 The Container Classes
|
---|
| 100 |
|
---|
| 101 | Qt provides the following sequential containers: QList,
|
---|
| 102 | QLinkedList, QVector, QStack, and QQueue. For most
|
---|
| 103 | applications, QList is the best type to use. Although it is
|
---|
| 104 | implemented as an array-list, it provides very fast prepends and
|
---|
| 105 | appends. If you really need a linked-list, use QLinkedList; if you
|
---|
| 106 | want your items to occupy consecutive memory locations, use QVector.
|
---|
| 107 | QStack and QQueue are convenience classes that provide LIFO and
|
---|
| 108 | FIFO semantics.
|
---|
| 109 |
|
---|
| 110 | Qt also provides these associative containers: QMap,
|
---|
| 111 | QMultiMap, QHash, QMultiHash, and QSet. The "Multi" containers
|
---|
| 112 | conveniently support multiple values associated with a single
|
---|
| 113 | key. The "Hash" containers provide faster lookup by using a hash
|
---|
| 114 | function instead of a binary search on a sorted set.
|
---|
| 115 |
|
---|
| 116 | As special cases, the QCache and QContiguousCache classes provide
|
---|
| 117 | efficient hash-lookup of objects in a limited cache storage.
|
---|
| 118 |
|
---|
| 119 | \table
|
---|
| 120 | \header \o Class \o Summary
|
---|
| 121 |
|
---|
| 122 | \row \o \l{QList}<T>
|
---|
| 123 | \o This is by far the most commonly used container class. It
|
---|
| 124 | stores a list of values of a given type (T) that can be accessed
|
---|
| 125 | by index. Internally, the QList is implemented using an array,
|
---|
| 126 | ensuring that index-based access is very fast.
|
---|
| 127 |
|
---|
| 128 | Items can be added at either end of the list using
|
---|
| 129 | QList::append() and QList::prepend(), or they can be inserted in
|
---|
| 130 | the middle using QList::insert(). More than any other container
|
---|
| 131 | class, QList is highly optimized to expand to as little code as
|
---|
| 132 | possible in the executable. QStringList inherits from
|
---|
| 133 | QList<QString>.
|
---|
| 134 |
|
---|
| 135 | \row \o \l{QLinkedList}<T>
|
---|
| 136 | \o This is similar to QList, except that it uses
|
---|
| 137 | iterators rather than integer indexes to access items. It also
|
---|
| 138 | provides better performance than QList when inserting in the
|
---|
| 139 | middle of a huge list, and it has nicer iterator semantics.
|
---|
| 140 | (Iterators pointing to an item in a QLinkedList remain valid as
|
---|
| 141 | long as the item exists, whereas iterators to a QList can become
|
---|
| 142 | invalid after any insertion or removal.)
|
---|
| 143 |
|
---|
| 144 | \row \o \l{QVector}<T>
|
---|
| 145 | \o This stores an array of values of a given type at adjacent
|
---|
| 146 | positions in memory. Inserting at the front or in the middle of
|
---|
| 147 | a vector can be quite slow, because it can lead to large numbers
|
---|
| 148 | of items having to be moved by one position in memory.
|
---|
| 149 |
|
---|
| 150 | \row \o \l{QStack}<T>
|
---|
| 151 | \o This is a convenience subclass of QVector that provides
|
---|
| 152 | "last in, first out" (LIFO) semantics. It adds the following
|
---|
| 153 | functions to those already present in QVector:
|
---|
| 154 | \l{QStack::push()}{push()}, \l{QStack::pop()}{pop()},
|
---|
| 155 | and \l{QStack::top()}{top()}.
|
---|
| 156 |
|
---|
| 157 | \row \o \l{QQueue}<T>
|
---|
| 158 | \o This is a convenience subclass of QList that provides
|
---|
| 159 | "first in, first out" (FIFO) semantics. It adds the following
|
---|
| 160 | functions to those already present in QList:
|
---|
| 161 | \l{QQueue::enqueue()}{enqueue()},
|
---|
| 162 | \l{QQueue::dequeue()}{dequeue()}, and \l{QQueue::head()}{head()}.
|
---|
| 163 |
|
---|
| 164 | \row \o \l{QSet}<T>
|
---|
| 165 | \o This provides a single-valued mathematical set with fast
|
---|
| 166 | lookups.
|
---|
| 167 |
|
---|
| 168 | \row \o \l{QMap}<Key, T>
|
---|
| 169 | \o This provides a dictionary (associative array) that maps keys
|
---|
| 170 | of type Key to values of type T. Normally each key is associated
|
---|
| 171 | with a single value. QMap stores its data in Key order; if order
|
---|
| 172 | doesn't matter QHash is a faster alternative.
|
---|
| 173 |
|
---|
| 174 | \row \o \l{QMultiMap}<Key, T>
|
---|
| 175 | \o This is a convenience subclass of QMap that provides a nice
|
---|
| 176 | interface for multi-valued maps, i.e. maps where one key can be
|
---|
| 177 | associated with multiple values.
|
---|
| 178 |
|
---|
| 179 | \row \o \l{QHash}<Key, T>
|
---|
| 180 | \o This has almost the same API as QMap, but provides
|
---|
| 181 | significantly faster lookups. QHash stores its data in an
|
---|
| 182 | arbitrary order.
|
---|
| 183 |
|
---|
| 184 | \row \o \l{QMultiHash}<Key, T>
|
---|
| 185 | \o This is a convenience subclass of QHash that
|
---|
| 186 | provides a nice interface for multi-valued hashes.
|
---|
| 187 |
|
---|
| 188 | \endtable
|
---|
| 189 |
|
---|
| 190 | Containers can be nested. For example, it is perfectly possible
|
---|
| 191 | to use a QMap<QString, QList<int> >, where the key type is
|
---|
| |
---|