Logo Search packages:      
Sourcecode: qt4-x11 version File versions  Download package

QVector< T > Class Template Reference

Inheritance diagram for QVector< T >:

QStack< T >

List of all members.


Detailed Description

template<typename T>
class QVector< T >

The QVector class is a template class that provides a dynamic array.

QVector<T> is one of Qt's generic {container classes}. It stores its items in adjacent memory locations and provides fast index-based access.

QList<T>, QLinkedList<T>, and QVarLengthArray<T> provide similar functionality. Here's an overview:

For most purposes, QList is the right class to use. Operations like prepend() and insert() are usually faster than with QVector because of the way QList stores its items in memory (see {Algorithmic Complexity} for details), and its index-based API is more convenient than QLinkedList's iterator-based API. It also expands to less code in your executable. If you need a real linked list, with guarantees of {constant time} insertions in the middle of the list and iterators to items rather than indexes, use QLinkedList. If you want the items to occupy adjacent memory positions, or if your items are larger than a pointer and you want to avoid the overhead of allocating them on the heap individually at insertion time, then use QVector. If you want a low-level variable-size array, QVarLengthArray may be sufficient.

Here's an example of a QVector that stores integers and a QVector that stores QString values:

        QVector<int> integerVector;
        QVector<QString> stringVector;

QVector stores a vector (or array) of items. Typically, vectors are created with an initial size. For example, the following code constructs a QVector with 200 elements:

        QVector<QString> vector(200);

The elements are automatically initialized with a {default-constructed value}. If you want to initialize the vector with a different value, pass that value as the second argument to the constructor:

        QVector<QString> vector(200, "Pass");

You can also call fill() at any time to fill the vector with a value.

QVector uses 0-based indexes, just like C++ arrays. To access the item at a particular index position, you can use operator[](). On non-const vectors, operator[]() returns a reference to the item that can be used on the left side of an assignment:

        if (vector[0] == "Liz")
            vector[0] = "Elizabeth";

For read-only access, an alternative syntax is to use at():

        for (int i = 0; i < vector.size(); ++i) {
            if (vector.at(i) == "Alfonso")
                cout << "Found Alfonso at position " << i << endl;
        }

at() can be faster than operator[](), because it never causes a {deep copy} to occur.

Another way to access the data stored in a QVector is to call data(). The function returns a pointer to the first item in the vector. You can use the pointer to directly access and modify the elements stored in the vector. The pointer is also useful if you need to pass a QVector to a function that accepts a plain C++ array.

If you want to find all occurrences of a particular value in a vector, use indexOf() or lastIndexOf(). The former searches forward starting from a given index position, the latter searches backward. Both return the index of the matching item if they found one; otherwise, they return -1. For example:

        int i = vector.indexOf("Harumi");
        if (i != -1)
            cout << "First occurrence of Harumi is at position " << i << endl;

If you simply want to check whether a vector contains a particular value, use contains(). If you want to find out how many times a particular value occurs in the vector, use count().

QVector provides these basic functions to add, move, and remove items: insert(), replace(), remove(), prepend(), append(). With the exception of append(), these functions can be slow ({linear time}) for large vectors, because they require moving many items in the vector by one position in memory. If you want a container class that provides fast insertion/removal in the middle, use QList or QLinkedList instead.

Unlike plain C++ arrays, QVectors can be resized at any time by calling resize(). If the new size is larger than the old size, QVector might need to reallocate the whole vector. QVector tries to reduce the number of reallocations by preallocating up to twice as much memory as the actual data needs.

If you know in advance approximately how many items the QVector will contain, you can call reserve(), asking QVector to preallocate a certain amount of memory. You can also call capacity() to find out how much memory QVector actually allocated.

QVector's value type must be an {assignable data type}. This covers most data types that are commonly used, but the compiler won't let you, for example, store a QWidget as a value; instead, store a QWidget *. A few functions have additional requirements; for example, indexOf() and lastIndexOf() expect the value type to support operator==(). These requirements are documented on a per-function basis.

Like the other container classes, QVector provides {Java-style iterators} (QVectorIterator and QMutableVectorIterator) and {STL-style iterators} (QVector::const_iterator and QVector::iterator). In practice, these are rarely used, because you can use indexes into the QVector.

In addition to QVector, Qt also provides QVarLengthArray, a very low-level class with little functionality that is optimized for speed.

QVector does not support inserting, prepending, appending or replacing with references to its own values. Doing so will cause your application to abort with an error message.

See also:
QVectorIterator, QMutableVectorIterator, QList, QLinkedList

Definition at line 100 of file qvector.h.


Public Types

typedef const T * const_iterator
typedef const value_typeconst_pointer
typedef const value_typeconst_reference
typedef const_iterator ConstIterator
typedef ptrdiff_t difference_type
typedef iterator Iterator
typedef T * iterator
typedef value_typepointer
typedef value_typereference
typedef int size_type
typedef T value_type

Public Member Functions

void append (const T &t)
const T & at (int i) const
const_reference back () const
reference back ()
const_iterator begin () const
iterator begin ()
int capacity () const
void clear ()
const_iterator constBegin () const
const T * constData () const
const_iterator constEnd () const
bool contains (const T &t) const
int count () const
int count (const T &t) const
const T * data () const
T * data ()
void detach ()
bool empty () const
const_iterator end () const
iterator end ()
iterator erase (iterator pos)
iterator erase (iterator begin, iterator end)
QVector< T > & fill (const T &t, int size=-1)
const T & first () const
T & first ()
const_reference front () const
T & front ()
int indexOf (const T &t, int from=0) const
iterator insert (iterator before, const T &x)
iterator insert (iterator before, int n, const T &x)
void insert (int i, int n, const T &t)
void insert (int i, const T &t)
bool isDetached () const
bool isEmpty () const
const T & last () const
T & last ()
int lastIndexOf (const T &t, int from=-1) const
QVector< T > mid (int pos, int length=-1) const
bool operator!= (const QVector< T > &v) const
QVector< T > operator+ (const QVector< T > &l) const
QVector< T > & operator+= (const T &t)
QVector< T > & operator+= (const QVector< T > &l)
QVector< T > & operator<< (const QVector< T > &l)
QVector< T > & operator<< (const T &t)
QVector< T > & operator= (const QVector< T > &v)
bool operator== (const QVector< T > &v) const
const T & operator[] (int i) const
T & operator[] (int i)
void pop_back ()
void pop_front ()
void prepend (const T &t)
void push_back (const T &t)
void push_front (const T &t)
 QVector (const QVector< T > &v)
 QVector (int size, const T &t)
 QVector (int size)
 QVector ()
void remove (int i, int n)
void remove (int i)
void replace (int i, const T &t)
void reserve (int size)
void resize (int size)
void setSharable (bool sharable)
int size () const
void squeeze ()
QList< T > toList () const
std::vector< T > toStdVector () const
value (int i, const T &defaultValue) const
value (int i) const
 ~QVector ()

Static Public Member Functions

static QVector< T > fromList (const QList< T > &list)
static QVector< T > fromStdVector (const std::vector< T > &vector)

Private Types

typedef QVectorTypedData< T > Data

Private Member Functions

void detach_helper ()
void free (Data *d)
QVectorData * malloc (int alloc)
void realloc (int size, int alloc)

Private Attributes

union {
   QVectorTypedData< T > *   d
   QVectorData *   p
}; 

Related Functions

(Note that these are not member functions.)

QDataStreamoperator<< (QDataStream &out, const QVector< T > &vector)
QDataStreamoperator>> (QDataStream &in, QVector< T > &vector)

The documentation for this class was generated from the following files:

Generated by  Doxygen 1.6.0   Back to index