| Classes - Annotated - Tree - Functions - Home - Structure

# QVector Class Reference

The QVector class is a template collection class that provides a vector (array). More...

`#include <qvector.h>`

Inherits QGVector.

List of all member functions.

## Public Members

- QVector ()
- QVector ( uint size )
- QVector ( const QVector<type> & v )
- ~QVector ()
- QVector<type>& operator= ( const QVector<type> & v )
- type** data () const
- uint size () const
- virtual uint count () const
- bool isEmpty () const
- bool isNull () const
- bool resize ( uint size )
- bool insert ( uint i, const type * d )
- bool remove ( uint i )
- type* take ( uint i )
- virtual void clear ()
- bool fill ( const type * d, int size=-1 )
- void sort ()
- int bsearch ( const type * d ) const
- int findRef ( const type * d, uint i=0 ) const
- int find ( const type * d, uint i= 0 ) const
- uint containsRef ( const type * d ) const
- uint contains ( const type * d ) const
- type* operator[] ( int i ) const
- type* at ( uint i ) const
- void toList ( QGList * list ) const

## Detailed Description

The QVector class is a template collection class that provides a vector (array).

QVector is implemented as a template class. Define a template instance QVector<X> to create a vector that contains pointers to X, or X*.

A vector is the same as an array. The main difference between QVector and QArray is that QVector stores pointers to the elements, while QArray stores the elements themselves (i.e. QArray is value-based).

Unless where otherwise stated, all functions that remove items from the vector will also delete the element pointed to if auto-deletion is enabled - see setAutoDelete(). By default, auto-deletion is disabled. This behaviour can be changed in a subclass by reimplementing the virtual method deleteItem().

Functions that compares items, e.g. find() and sort(), will do so using the virtual function compareItems(). The default implementation of this function will only compare the absolute pointer values. Reimplement compareItems() in a subclass to get searching and sorting based on the item contents.

See also Collection Classes and QArray.

## Member Function Documentation

### QVector::QVector ()

Constructs a null vector.

See also isNull().

### QVector::QVector ( const QVector<type> & v )

Constructs a copy of v. Only the pointers are copied (i.e. shallow copy).

### QVector::QVector ( uint size )

Constructs an vector with room for size items. Makes a null vector if size == 0.

All size positions in the vector are initialized to 0.

See also size(), resize() and isNull().

### QVector::~QVector ()

Removes all items from the vector, and destroys the vector itself.

See also clear().

### type * QVector::at ( uint i ) const

Returns the item at position i, or 0 if there is no item at that position. i must be less than size().

### int QVector::bsearch ( const type * d ) const

In a sorted array, finds the first occurrence of d using binary search. For a sorted array, this is generally much faster than find(), which does a linear search.

Returns the position of d, or -1 if d could not be found. d may not be 0.

Compares items using the virtual function compareItems().

See also sort() and find().

### void QVector::clear () `[virtual]`

Removes all items from the vector, and destroys the vector itself.

The vector becomes a null vector.

See also isNull().

Reimplemented from QCollection.

### uint QVector::contains ( const type * d ) const

Returns the number of occurrences of item d in the vector.

Compares items using the virtual function compareItems().

See also containsRef().

### uint QVector::containsRef ( const type * d ) const

Returns the number of occurrences of the item pointer d in the vector.

This function does not use compareItems() to compare items.

See also findRef().

### uint QVector::count () const `[virtual]`

Returns the number of items in the vector. The vector is empty if count() == 0.

See also isEmpty() and size().

Reimplemented from QCollection.

### type ** QVector::data () const

Returns a pointer to the actual vector data, which is an array of type*.

The vector is a null vector if data() == 0 (null pointer).

See also isNull().

### bool QVector::fill ( const type * d, int size=-1 )

Inserts item d in all positions in the vector. Any existing items are removed. If d is 0, the vector becomes empty.

If size >= 0, the vector is first resized to size. By default, size is -1.

Returns TRUE if successful, or FALSE if the memory cannot be allocated (only if a resize has been requested).

See also resize(), insert() and isEmpty().

### int QVector::find ( const type * d, uint i= 0 ) const

Finds the first occurrence of item d in the vector, using linear search. The search starts at position i, which must be less than size(). i is by default 0; i.e. the search starts at the start of the vector.

Returns the position of v, or -1 if v could not be found.

Compares items using the virtual function compareItems().

See also findRef() and bsearch().

### int QVector::findRef ( const type * d, uint i=0 ) const

Finds the first occurrence of the item pointer d in the vector, using linear search. The search starts at position i, which must be less than size(). i is by default 0; i.e. the search starts at the start of the vector.

Returns the position of d, or -1 if d could not be found.

This function does not use compareItems() to compare items.

See also find() and bsearch().

### bool QVector::insert ( uint i, const type * d )

Sets position i in the vector to contain the item d. i must be less than size(). Any previous element in position i is removed.

See also at().

### bool QVector::isEmpty () const

Returns TRUE if the vector is empty, i.e. count() == 0, otherwise FALSE.

See also count().

### bool QVector::isNull () const

Returns TRUE if the vector is null, otherwise FALSE.

A null vector has size() == 0 and data() == 0.

See also size().

### QVector<type> & QVector::operator= ( const QVector<type> & v )

Assigns v to this vector and returns a reference to this vector.

This vector is first cleared, then all the items from v is copied into this vector. Only the pointers are copied (i.e. shallow copy).

See also clear().

### type * QVector::operator[] ( int i ) const

Returns the item at position i, or 0 if there is no item at that position. i must be less than size().

Equivalent to at( i ).

See also at().

### bool QVector::remove ( uint i )

Removes the item at position i in the vector, if there is one. i must be less than size().

Returns TRUE unless i is out of range.

See also take() and at().

### bool QVector::resize ( uint size )

Resizes (expands or shrinks) the vector to size elements. The array becomes a null array if size == 0.

Any items in position size or beyond in the vector are removed. New positions are initialized 0.

Returns TRUE if successful, or FALSE if the memory cannot be allocated.

See also size() and isNull().

### uint QVector::size () const

Returns the size of the vector, i.e. the number of vector positions. This is also the maximum number of items the vector can hold.

The vector is a null vector if size() == 0.

See also isNull(), resize() and count().

### void QVector::sort ()

Sorts the items in ascending order. Any empty positions will be put last.

Compares items using the virtual function compareItems().

See also bsearch().

### type* QVector::take ( uint i )

Returns the item at position i in the vector, and removes that item from the vector. i must be less than size(). If there is no item at position i, 0 is returned.

In contrast to remove(), this function does not call deleteItem() for the removed item.

See also remove() and at().

### void QVector::toList ( QGList * list ) const

Copies all items in this vector to the list list. First, list is cleared, then all items are appended to list.

See also QList, QStack and QQueue.

Search the documentation, FAQ, qt-interest archive and more (uses www.trolltech.com):

This file is part of the Qt toolkit, copyright © 1995-2000 Trolltech, all rights reserved.