QValueList Class ReferenceThe QValueList class is a value-based template class that provides lists. More... All the functions in this class are reentrant when Qt is built with thread support. #include <qvaluelist.h> Inherited by QCanvasItemList, QStringList, and QValueStack. Public Members
Related Functions
Detailed DescriptionThe QValueList class is a value-based template class that provides lists.
QValueList is a Qt implementation of an STL-like list container. It can be used in your application if the standard list is not available for your target platform(s). QValueList is part of the Qt Template Library. QValueList<T> defines a template instance to create a list of values that all have the class T. Note that QValueList does not store pointers to the members of the list; it holds a copy of every member. This is why these kinds of classes are called "value based"; QPtrList and QDict are "pointer based". QValueList contains and manages a collection of objects of type T and provides iterators that allow the contained objects to be addressed. QValueList owns the contained items. For more relaxed ownership semantics, see QPtrCollection and friends which are pointer-based containers. Some classes cannot be used within a QValueList, for example, all classes derived from QObject and thus all classes that implement widgets. Only values can be used in a QValueList. To qualify as a value the class must provide:
Note that C++ defaults to field-by-field assignment operators and copy constructors if no explicit version is supplied. In many cases this is sufficient. In addition, some compilers (e.g. Sun CC) might require that the class provides an equality operator (operator==()). QValueList's function naming is consistent with the other Qt classes (e.g. count(), isEmpty()). QValueList also provides extra functions for compatibility with STL algorithms, such as size() and empty(). Programmers already familiar with the STL list may prefer to use the STL-compatible functions. Example: class Employee { public: Employee(): sn(0) {} Employee( const QString& forename, const QString& surname, int salary ) : fn(forename), sn(surname), sal(salary) {} QString forename() const { return fn; } QString surname() const { return sn; } int salary() const { return sal; } void setSalary( int salary ) { sal = salary; } private: QString fn; QString sn; int sal; }; typedef QValueList<Employee> EmployeeList; EmployeeList list; list.append( Employee("John", "Doe", 50000) ); list.append( Employee("Jane", "Williams", 80000) ); list.append( Employee("Tom", "Jones", 60000) ); Employee mary( "Mary", "Hawthorne", 90000 ); list.append( mary ); mary.setSalary( 100000 ); EmployeeList::iterator it; for ( it = list.begin(); it != list.end(); ++it ) cout << (*it).surname().latin1() << ", " << (*it).forename().latin1() << " earns " << (*it).salary() << endl; // Output: // Doe, John earns 50000 // Williams, Jane earns 80000 // Hawthorne, Mary earns 90000 // Jones, Tom earns 60000 Notice that the latest changes to Mary's salary did not affect the value in the list because the list created a copy of Mary's entry. There are several ways to find items in the list. The begin() and end() functions return iterators to the beginning and end of the list. The advantage of getting an iterator is that you can move forward or backward from this position by incrementing/decrementing the iterator. The iterator returned by end() points to the item which is one past the last item in the container. The past-the-end iterator is still associated with the list it belongs to, however it is not dereferenceable; operator*() will not return a well-defined value. If the list is empty(), the iterator returned by begin() will equal the iterator returned by end(). Another way to find an item in the list is by using the qFind() algorithm. For example:
QValueList<int> list; ... QValueList<int>::iterator it = qFind( list.begin(), list.end(), 3 ); if ( it != list.end() ) // it points to the found item It is safe to have multiple iterators a the list at the same time. If some member of the list is removed, only iterators pointing to the removed member become invalid. Inserting into the list does not invalidate any iterator. For convenience, the function last() returns a reference to the last item in the list, and first() returns a reference to the the first item. If the list is empty(), both last() and first() have undefined behavior (your application will crash or do unpredictable things). Use last() and first() with caution, for example:
QValueList<int> list; list.append( 1 ); list.append( 2 ); list.append( 3 ); ... if ( !list.empty() ) { // OK, modify the first item int& i = list.first(); i = 18; } ... QValueList<double> dlist; double d = dlist.last(); // undefined Because QValueList is value-based there is no need to be careful about deleting items in the list. The list holds its own copies and will free them if the corresponding member or the list itself is deleted. You can force the list to free all of its items with clear(). QValueList is shared implicitly, which means it can be copied in constant time, i.e. O(1). If multiple QValueList instances share the same data and one needs to modify its contents, this modifying instance makes a copy and modifies its private copy; therefore it does not affect the other instances; this takes O(n) time. This is often called "copy on write". If a QValueList is being used in a multi-threaded program, you must protect all access to the list. See QMutex. There are several ways to insert items into the list. The prepend() and append() functions insert items at the beginning and the end of the list respectively. The insert() function comes in several flavors and can be used to add one or more items at specific positions within the list. Items can also be removed from the list in several ways. There are several variants of the remove() function, which removes a specific item from the list. The remove() function will find and remove items according to a specific item value. Lists can also be sorted using the Qt Template Library. For example with qHeapSort(): Example: QValueList<int> list; list.append( 5 ); list.append( 8 ); list.append( 3 ); list.append( 4 ); qHeapSort( list );
See also QValueListIterator, Qt Template Library Classes, Implicitly and Explicitly Shared Classes, and Non-GUI Classes. Member Type Documentation
|
Publicité
Best OfActualités les plus luesSemaine
Mois
Année
Le Qt Quarterly au hasardRéordonner les boutonsQt Quarterly est la revue trimestrielle proposée par Nokia et à destination des développeurs Qt. Ces articles d'une grande qualité technique sont rédigés par des experts Qt. 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.3 | |
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