<QtAlgorithms> - Generic AlgorithmsThe <QtAlgorithms> header file provides generic template-based algorithms. More... Functions
Detailed DescriptionThe <QtAlgorithms> header file provides generic template-based algorithms. Qt provides a number of global template functions in <QtAlgorithms> that work on containers and perform well-know algorithms. You can use these algorithms with any container class that provides STL-style iterators, including Qt's QList, QLinkedList, QVector, QMap, and QHash classes. These functions have taken their inspiration from similar functions available in the STL <algorithm> header. Most of them have a direct STL equivalent; for example, qCopyBackward() is the same as STL's copy_backward() algorithm. If STL is available on all your target platforms, you can use the STL algorithms instead of their Qt counterparts. One reason why you might want to use the the STL algorithms is that STL provides dozens and dozens of algorithms, whereas Qt only provides the most important ones, making no attempt to duplicate functionality that is already provided by the C++ standard. Most algorithms take STL-style iterators as parameters. The algorithms are generic in the sense that they aren't bound to a specific iterator class; you can use them with any iterators that meet a certain set of requirements. Let's take the qFill() algorithm as an example. Unlike QVector, QList has no fill() function that can be used to fill a list with a particular value. If you need that functionality, you can use qFill(): QStringList list; list << "one" << "two" << "three"; qFill(list.begin(), list.end(), "eleven"); // list: [ "eleven", "eleven", "eleven" ] qFill() takes a begin iterator, an end iterator, and a value. In the example above, we pass list.begin() and list.end() as the begin and end iterators, but this doesn't have to be the case: qFill(list.begin() + 1, list.end(), "six"); // list: [ "eleven", "six", "six" ] The various algorithms have different requirements for the iterators they accept. For example, qFill() accepts two input iterators, the most minimal requirement for an iterator type. The requirements are specified for every algorithm. If an iterator of the wrong type is passed (for example, QList::ConstIterator is passed as an output iterator), you will always get a compiler error, although not necessarily a very informative one. Some algorithms have special requirements on the value type stored in the containers. For example, qEqual() requires that the value type supports operator==(), which it uses to compare items. Similarly, qDeleteAll() requires that the value type is a non-const pointer type (for example, QWidget *). The value type requirements are specified for each algorithm, and the compiler will produce an error if a requirement isn't met. The generic algorithms can be used on other container classes than those provided by Qt and STL. The syntax of STL-style iterators is modeled after C++ pointers, so it's possible to use plain arrays as containers and plain pointers as iterators. A common idiom is to use qBinaryFind() together with two static arrays: one that contains a list of keys, and another that contains a list of associated values. For example, the following code will look up an HTML entity (e.g., &) in the name_table array and return the corresponding Unicode value from the value_table if the entity is recognized: QChar resolveEntity(const QString &entity) { static const QLatin1String name_table[] = { "AElig", "Aacute", ..., "zwnj" }; static const ushort value_table[] = { 0x0061, 0x00c1, ..., 0x200c }; int N = sizeof(name_table) / sizeof(name_table[0]); const QLatin1String *name = qBinaryFind(name_table, name_table + N, entity); int index = name - name_table; if (index == N) return QChar(); return QChar(value_table[index]); } This kind of code is for advanced users only; for most applications, a QMap- or QHash-based approach would work just as well: QChar resolveEntity(const QString &entity) { static QMap<QString, int> entityMap; if (!entityMap) { entityMap.insert("AElig", 0x0061); entityMap.insert("Aacute", 0x00c1); ... entityMap.insert("zwnj", 0x200c); } return QChar(entityMap.value(entity)); } Types of IteratorsThe algorithms have certain requirements on the iterator types they accept, and these are specified individually for each function. The compiler will produce an error if a requirement isn't met. Input IteratorsAn input iterator is an iterator that can be used for reading data sequentially from a container. It must provide the following operators: == and != for comparing two iterators, unary * for retrieving the value stored in the item, and prefix ++ for advancing to the next item. The Qt containers' iterator types (const and non-const) are all input iterators. Output IteratorsAn output iterator is an iterator that can be used for writing data sequentially to a container or to some output stream. It must provide the following operators: unary * for writing a value (i.e., *it = val) and prefix ++ for advancing to the next item. The Qt containers' non-const iterator types are all output iterators. Forward IteratorsA forward iterator is an iterator that meets the requirements of both input iterators and output iterators. The Qt containers' non-const iterator types are all forward iterators. Bidirectional IteratorsA bidirectional iterator is an iterator that meets the requirements of forward iterators but that in addition supports prefix -- for iterating backward. The Qt containers' non-const iterator types are all bidirectional iterators. Random Access IteratorsThe last category, random access iterators, is the most powerful type of iterator. It supports all the requirements of a bidirectional iterator, and supports the following operations:
QList, QLinkedList, and QVector's non-const iterator types are random access iterators. See also container classes and <QtGlobal>. Function Documentation
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Developer Network au hasardComment fermer une applicationLe 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 4.1 | |
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