TQValueVector is a TQt implementation of an STL-like vector container. It can be used in your application if the standard \fCvector\fR is not available for your target platforms. TQValueVector is part of the TQt Template Library.
TQValueVector<T> defines a template instance to create a vector of values that all have the class T. TQValueVector does not store pointers to the members of the vector; it holds a copy of every member. TQValueVector is said to be value based; in contrast, TQPtrList and QDict are pointer based.
TQValueVector contains and manages a collection of objects of type T and provides random access iterators that allow the contained objects to be addressed. TQValueVector owns the contained elements. For more relaxed ownership semantics, see TQPtrCollection and friends, which are pointer-based containers.
TQValueVector 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 TQValueVector: for example, all classes derived from TQObject and thus all classes that implement widgets. Only values can be used in a TQValueVector. To qualify as a value the class must provide:
a default constructor, i.e., a constructor that does not take any arguments.
.PP
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.
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 \fInot\fR 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().
.PP
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:
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.
It is safe to have multiple iterators on the vector at the same time. Since TQValueVector 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:
Because TQValueVector 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 TQValueVector 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 TQValueVector 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().
TQValueVector is shared implicitly, which means it can be copied in constant time. If multiple TQValueVector 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 TQValueVector is being used in a multi-threaded program, you must protect all access to the vector. See TQMutex.
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.
.PP
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.
Constructs an empty vector without any elements. To create a vector which reserves an initial amount of space for elements, use \fCTQValueVector(size_type n)\fR.
.SH "TQValueVector::TQValueVector ( const TQValueVector<T> & v )"
Destroys the vector, destroying all elements and freeing the allocated memory. References to the values in the vector and all iterators of this vector become invalidated. Note that it is impossible for an iterator to check whether or not it is valid: TQValueVector is tuned for performance, not for error checking.
Returns a reference to the element with index \fIi\fR. If \fIok\fR is non-null, and the index \fIi\fR is out of range, *\fIok\fR is set to FALSE and the returned reference is undefined. If the index \fIi\fR is within the range of the vector, and \fIok\fR is non-null, *\fIok\fR is set to TRUE and the returned reference is well defined.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
.PP
Returns a const reference to the element with index \fIi\fR. If \fIok\fR is non-null, and the index \fIi\fR is out of range, *\fIok\fR is set to FALSE and the returned reference is undefined. If the index \fIi\fR is within the range of the vector, and \fIok\fR is non-null, *\fIok\fR is set to TRUE and the returned reference is well defined.
Returns the maximum number of elements that can be stored in the vector without forcing memory reallocation. If memory reallocation takes place, some or all iterators may become invalidated.
All iterators of the current vector become invalidated by this operation. The cost of such an assignment is O(1) since TQValueVector is implicitly shared.
.SH "TQValueVector<T> & TQValueVector::operator= ( const std::vector<T> & v )"
Increases the vector's capacity. If \fIn\fR is less than or equal to capacity(), nothing happens. Otherwise, additional memory is allocated so that capacity() will be increased to a value greater than or equal to \fIn\fR. All iterators will then become invalidated. Note that the vector's size() and the values of existing elements remain unchanged.
Changes the size of the vector to \fIn\fR. If \fIn\fR is greater than the current size(), elements are added to the end and initialized with the value of \fIval\fR. If \fIn\fR is less than size(), elements are removed from the end. If \fIn\fR is equal to size() nothing happens.