Qt Template Library
The Qt Template Library (QTL) is a set of templates that provide object containers. If a suitable STL implementation is not available on all your target platforms, the QTL can be used instead. It provides a list of objects, a vector (dynamic array) of objects, a map relating one type to another (also called a dictionary or associative array), and associated iterators and algorithms. A container is an object which contains and manages other objects and provides iterators that allow the contained objects to be accessed. The QTL classes' naming conventions are consistent with the other Qt classes (e.g., count(), isEmpty()). They also provide extra functions for compatibility with STL algorithms, such as size() and empty(). Programmers already familiar with the STL map can use the STL-compatible functions if preferred. Compared to the STL, the QTL only contains the most important features of the STL container API. Compared with the STL, QTL has no platform differences, but is often a little slower and often expands to less object code. If you cannot make copies of the objects you want to store you should use QPtrCollection and friends, all of which operate on pointers rather than values. This applies, for example, to all classes derived from QObject. A QObject does not have a copy constructor, so using it as value is impossible. You may choose to store pointers to QObjects in a QValueList, but using QPtrList directly seems to be the better choice for this kind of application domain. QPtrList, like all other QPtrCollection based containers, provides far more sanity checking than a speed-optimized value based container. If you have objects that implement value semantics, and the STL is not available on your target platform, the Qt Template Library can be used instead. Value semantics require at least:
Note that a fast copy constructor is absolutely crucial to achieve good overall performance of the container, since many copy operations will occur. If you intend sorting your data you must implement operator<() for your data's class. Good candidates for value based classes are QRect, QPoint, QSize, QString and all simple C++ types, such as int, bool or double. The Qt Template Library is designed for speed. Iterators are extremely fast. To achieve this performance, less error checking is done than in the QPtrCollection based containers. A QTL container, for example, does not track any associated iterators. This makes certain validity checks, for example when removing items, impossible to perform automatically, but does lead to extremely good performance. The Qt Template Library deals with value objects, not with pointers.
For that reason, there is no other way of iterating over containers
other than with iterators. This is no disadvantage as the size of an
iterator matches the size of a normal pointer.
To iterate over a container, use a loop like this:
begin() returns the iterator pointing at the first element, while
end() returns an iterator that points after the last element. end()
marks an invalid position, so it can never be dereferenced. It's the
break condition in any iteration, whether the start point is from
begin() or fromLast(). For maximum speed, use increment or decrement
iterators with the prefix operator (++it, --it) instead of the postfix
operator (it++, it--), since the former is slightly faster.
The same concept applies to the other container classes:
There are two kind of iterators, the volatile iterator shown in the
examples above and a version that returns a const reference to its
current object, the ConstIterator. Const iterators are required
whenever the container itself is const, such as a member variable
inside a const function. Assigning a ConstIterator to a normal
Iterator is not allowed as it would violate const semantics.
The Qt Template Library defines a number of algorithms that operate on
its containers. These algorithms are implemented as template functions
and provide useful generic code which can be applied to any container
that provides iterators (including your own containers).
qHeapSort() provides a well known sorting algorithm. You can use it
like this:
The first example sorts the entire list. The second example sorts only
those elements that fall between the two iterators, i.e. 100, 1234 and
12. The third example shows that iterators act like pointers and can
be treated as such.
If using your own data types you must implement operator<() for
your data's class.
Naturally, the sorting templates won't work with const iterators.
qSwap() exchanges the values of two variables:
The qCount() template function counts the number of occurrences of a
value within a container. For example:
The qFind() template function finds the first occurrence of a value
within a container. For example:
The qFill() template function fills a range with copies of a value.
For example:
The qEqual() template function compares two ranges for equality of
their elements. Note that the number of elements in each range is not
considered, only if the elements in the first range are equal to the
corresponding elements in the second range (consequently, both ranges
must be valid). For example:
The qCopy() template function copies a range of elements to an
OutputIterator, in this case a QTextOStreamIterator:
The qCopyBackward() template function copies a container or a slice of
a container to an OutputIterator, but in reverse order, for example:
You can use any Qt Template Library iterator as the OutputIterator.
Just make sure that the right hand of the iterator has as many
elements present as you want to insert. The following example
illustrates this:
At the end of this code fragment, the list list1 contains "Torben",
"Matthias", "Arnt" and "Sue", with the prior contents being
overwritten. The vector vec contains "Torben", "Matthias", "Dave" and
"Dave", also with the prior contents being overwritten.
If you write new algorithms, consider writing them as template
functions in order to make them usable with as many containers
as possible. In the above example, you could just as easily print out
a standard C++ array with qCopy():
All the containers we've mentioned can be serialized with the
appropriate streaming operators. Here is an example.
The container can be read in again with:
The same applies to QStringList, QValueStack and QMap.
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Developer Network au hasardGénération de bindings PySide avec ShibokenLe Qt Developer Network est un réseau de développeurs Qt anglophone, où ils peuvent partager leur expérience sur le framework. Lire l'article.
CommunautéRessources
Liens utilesContact
Qt dans le magazine |
Cette page est une traduction d'une page de la documentation de Qt, écrite par Nokia Corporation and/or its subsidiary(-ies). Les éventuels problèmes résultant d'une mauvaise traduction ne sont pas imputables à Nokia. | Qt 3.2 | |
Copyright © 2012 Developpez LLC. Tous droits réservés Developpez LLC. Aucune reproduction, même partielle, ne peut être faite de ce site et de l'ensemble de son contenu : textes, documents et images sans l'autorisation expresse de Developpez LLC. Sinon, vous encourez selon la loi jusqu'à 3 ans de prison et jusqu'à 300 000 E de dommages et intérêts. Cette page est déposée à la SACD. | ||
Vous avez déniché une erreur ? Un bug ? Une redirection cassée ? Ou tout autre problème, quel qu'il soit ? Ou bien vous désirez participer à ce projet de traduction ? N'hésitez pas à nous contacter ou par MP ! |
Copyright © 2000-2012 - www.developpez.com