[ fromfile: inheritance-intro.xml id: containers ]
Qt's container classes collect value types (things that can be copied), including pointers to object types[49] (but not object types themselves). Qt containers are defined as template classes that leave the collected type unspecified. Each data structure is optimized for different kinds of operations. In Qt there are several template container classes to choose from.
QList<T> is implemented using an array, with space preallocated at both ends. It is optimized for index-based random access and, for lists with less than a thousand items, it also gives good performance with operations like prepend()
and append()
.
QStringList is a convenience class derived from QList<QString>.
QLinkedList<T> is optimized for sequential access with iterators and quick, constant-time inserts anywhere in the list. Sorting and searching are slow. It has several convenience functions for frequently used operations.
QVector<T> stores its data in contiguous memory locations and is optimized for random access by index. Generally, QVector objects are constructed with an initial size. There is no automatic preallocation of memory at either end so insertions, appends, and prepends are expensive.
QStack<T> is publicly derived from QVector<T> so the public interface of QVector is available to QStack objects. However, the last-in-first-out semantics are offered by the push(), pop()
, and top()
functions.
QMap<Key,T> is an ordered associative container that stores (key, value) pairs and is designed for fast lookup of the value associated with a key. It is also designed to support reasonably fast insertions and removals. It keeps its keys in sorted order, for fast searching and subranging, by means of a skip-list dictionary that is probabilistically balanced and uses memory efficiently. The Key type must have an operator<()
and operator==()
.
QHash<Key,T> is also an associative container that uses a hash table to facilitate key lookups. It provides fast lookups (exact key match) and insertions, but slow searching, and no sorting. The Key type must have an operator==()
.
QMultiMap<Key,T> is a subclass of QMap and QMultiHash<Key,T> is a subclass of QHash. These two classes enable multiple values to be associated with a single key.
QCache<Key,T> is an associative container that provides fastest access to recently used items and automatic removal of infrequently used items based on cost functions.
QSet<T> stores values of type T using a QHash with keys in T and a dummy value associated with each key. This arrangement optimizes lookups and insertions. QSet has functions for the usual set operations (e.g., union, intersection, set difference, etc.). The default constructor creates an empty set.
A type parameter T
for a template container class or key type for an associative container must be an assignable data type (i.e., a value type). (Section 8.1).
This means that T
must have a public
default constructor, copy constructor, and assignment operator.
Basic types (e.g, int
, double
, char
, etc.) and pointers are assignable.
Some Qt types are assignable (e.g., QString, QDate, QTime).
QObject and types derived from QObject, however, are not assignable.
If you need to collect objects of some nonassignable type, you can define a container of pointers, e.g., QList<QFile*>.
Generated: 2012-03-02 | © 2012 Alan Ezust and Paul Ezust. |