Containers
Qt's container classes are used to collect value types (things that can be copied), including pointers to object types (but not object types themselves). Qt containers are defined as template classes which leave the collected type unspecified. Each data structure is optimized for different kinds of operations. In Qt 4, there are several template container classes to choose from.
- QList 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 that is derived from QList.
- QLinkedList 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 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 is publicly derived from QVector, so the public interface of QVector is available to QStack objects. However, the last-in-first-out semantics are implemented by the push(), pop(), and top() functions.
- QMap is an ordered associative container that stores (key, value) pairs and is designed for fast lookup of the value associated with a key and for easy insertions. It keeps its keys in sorted order, for fast searching and subranging, by means of a skip-list dictionary[2] that is probabilistically balanced and uses memory very efficiently. The Key type must have an operator<() and operator==().
[2] ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf
- QHash is an associative container that uses a hash table to facilitate key lookups. It provides very fast lookups (exact key match) and insertions, but slow searching and no sorting. The Key type must have an operator==().
- QMultiMap is a subclass of QMap, and QMultiHash is a subclass of QHash. These two classes allow multiple values to be associated with a single key.
- QCache is also an associative container but it provides fastest access to recently used items and automatic removal of infrequently used items based on cost functions.
- QSet 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; see Section 9.5). This means that it 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, QTimer). QObject and types derived from QObject are not assignable. If you need to group objects of some non-assignable type, you can define a container of pointers, e.g., QList.