QValueVector Class ReferenceThe QValueVector class is a value-based template class that provides a dynamic array. More... All the functions in this class are reentrant when Qt is built with thread support. #include <qvaluevector.h> Public Members
Detailed DescriptionThe QValueVector class is a value-based template class that provides a dynamic array.
QValueVector is a Qt implementation of an STL-like vector container. It can be used in your application if the standard vector is not available for your target platforms. QValueVector is part of the Qt Template Library. QValueVector<T> defines a template instance to create a vector of values that all have the class T. QValueVector does not store pointers to the members of the vector; it holds a copy of every member. QValueVector is said to be value based; in contrast, QPtrList and QDict are pointer based. QValueVector contains and manages a collection of objects of type T and provides random access iterators that allow the contained objects to be addressed. QValueVector owns the contained elements. For more relaxed ownership semantics, see QPtrCollection and friends, which are pointer-based containers. QValueVector provides good performance if you append or remove elements from the end of the vector. If you insert or remove elements from anywhere but the end, performance is very bad. The reason for this is that elements must to be copied into new positions. Some classes cannot be used within a QValueVector: for example, all classes derived from QObject and thus all classes that implement widgets. Only values can be used in a QValueVector. 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. QValueVector uses an STL-like syntax to manipulate and address the objects it contains. See this document for more information. Example: #include <qvaluevector.h> #include <qstring.h> #include <stdio.h> class Employee { public: Employee(): s(0) {} Employee( const QString& name, int salary ) : n( name ), s( salary ) { } QString name() const { return n; } int salary() const { return s; } void setSalary( int salary ) { s = salary; } QString name() const { return n; } int salary() const { return s; } void setSalary( int salary ) { s = salary; } private: QString n; int s; }; int main() { typedef QValueVector<Employee> EmployeeVector; EmployeeVector vec( 3 ); // vector of 3 Employees vec[0] = Employee( "Bill", 50000 ); vec[1] = Employee( "Steve", 80000 ); vec[2] = Employee( "Ron", 60000 ); Employee joe( "Joe", 50000 ); vec.push_back( joe ); // vector expands to accommodate 4 Employees joe.setSalary( 70000 ); EmployeeVector::iterator it; for( it = vec.begin(); it != vec.end(); ++it ) printf( "%s earns %d\n", (*it).name().latin1(), (*it).salary() ); return 0; } Program output: Bill earns 50000 Steve earns 80000 Ron earns 60000 Joe earns 50000 As you can see, the most recent change to Joe's salary did not affect the value in the vector because the vector created a copy of Joe's entry. Many Qt functions return const value vectors; to iterate over these you should make a copy and iterate over the copy. There are several ways to find items in the vector. The begin() and end() functions return iterators to the beginning and end of the vector. 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 element which is one past the last element in the container. The past-the-end iterator is still associated with the vector it belongs to, however it is not dereferenceable; operator*() will not return a well-defined value. If the vector is empty(), the iterator returned by begin() will equal the iterator returned by end(). The fastest way to access an element of a vector is by using operator[]. This function provides random access and will return a reference to the element located at the specified index. Thus, you can access every element directly, in constant time, providing you know the location of the element. It is undefined to access an element that does not exist (your application will probably crash). For example:
QValueVector<int> vec1; // an empty vector vec1[10] = 4; // WARNING: undefined, probably a crash QValueVector<QString> vec2(25); // initialize with 25 elements vec2[10] = "Dave"; // OK Whenever inserting, removing or referencing elements in a vector, always make sure you are referring to valid positions. For example:
void func( QValueVector<int>& vec ) { if ( vec.size() > 10 ) { vec[9] = 99; // OK } }; The iterators provided by vector are random access iterators, therefore you can use them with many generic algorithms, for example, algorithms provided by the STL or the QTL. Another way to find an element in the vector is by using the std::find() or qFind() algorithms. For example:
QValueVector<int> vec; ... QValueVector<int>::const_iterator it = qFind( vec.begin(), vec.end(), 3 ); if ( it != vector.end() ) // 'it' points to the found element It is safe to have multiple iterators on the vector at the same time. Since QValueVector manages memory dynamically, all iterators can become invalid if a memory reallocation occurs. For example, if some member of the vector is removed, iterators that point to the removed element and to all following elements become invalidated. Inserting into the middle of the vector will invalidate all iterators. For convenience, the function back() returns a reference to the last element in the vector, and front() returns a reference to the first element. If the vector is empty(), both back() and front() have undefined behavior (your application will crash or do unpredictable things). Use back() and front() with caution, for example:
QValueVector<int> vec( 3 ); vec.push_back( 1 ); vec.push_back( 2 ); vec.push_back( 3 ); ... if ( !vec.empty() ) { // OK: modify the first element int& i = vec.front(); i = 18; } ... QValueVector<double> dvec; double d = dvec.back(); // undefined behavior Because QValueVector manages memory dynamically, it is recommended that you contruct a vector with an initial size. Inserting and removing elements happens fastest when:
By creating a QValueVector with a sufficiently large initial size, there will be less memory allocations. Do not use an initial size that is too big, since it will still take time to construct all the empty entries, and the extra space will be wasted if it is never used. Because QValueVector is value-based there is no need to be careful about deleting elements in the vector. The vector holds its own copies and will free them if the corresponding member or the vector itself is deleted. You can force the vector to free all of its items with clear(). QValueVector is shared implicitly, which means it can be copied in constant time. If multiple QValueVector instances share the same data and one needs to modify its contents, this modifying instance makes a copy and modifies its private copy; it thus does not affect the other instances. This is often called "copy on write". If a QValueVector is being used in a multi-threaded program, you must protect all access to the vector. See QMutex. There are several ways to insert elements into the vector. The push_back() function insert elements into the end of the vector, and is usually fastest. The insert() function can be used to add elements at specific positions within the vector. Items can be also be removed from the vector in several ways. There are several variants of the erase() function which removes a specific element, or range of elements, from the vector. Vectors can be also sorted with various STL algorithms , or it can be sorted using the Qt Template Library. For example with qHeapSort(): Example: QValueVector<int> v( 4 ); v.push_back( 5 ); v.push_back( 8 ); v.push_back( 3 ); v.push_back( 4 ); qHeapSort( v ); QValueVector stores its elements in contiguous memory. This means that you can use a QValueVector in any situation that requires an array. See also Qt Template Library Classes, Implicitly and Explicitly Shared Classes, and Non-GUI Classes. Member Type Documentation
The vector's const iterator type.
|
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