Home | All Classes | Main Classes | Annotated | Grouped Classes | Functions

TQPtrVector Class Reference

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

#include <tqptrvector.h>

Inherits TQPtrCollection.

List of all member functions.

Public Members

Important Inherited Members

Protected Members


Detailed Description

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

TQValueVector is an STL-compatible alternative to this class.

TQPtrVector is implemented as a template class. Defines a template instance TQPtrVector<X> to create a vector that contains pointers to X (X*).

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

Items are added to the vector using insert() or fill(). Items are removed with remove(). You can get a pointer to an item at a particular index position using at().

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

Functions that compare items (find() and sort() for example) will do so using the virtual function compareItems(). The default implementation of this function only compares the pointer values. Reimplement compareItems() in a subclass to get searching and sorting based on the item contents. You can perform a linear search for a pointer in the vector using findRef(), or a binary search (of a sorted vector) using bsearch(). You can count the number of times an item appears in the vector with contains() or containsRef().

See also TQMemArray and Non-GUI Classes.


Member Function Documentation

TQPtrVector::TQPtrVector ()

Constructs a null vector.

See also isNull().

TQPtrVector::TQPtrVector ( 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().

TQPtrVector::TQPtrVector ( const TQPtrVector<type> & v )

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

TQPtrVector::~TQPtrVector ()

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

See also clear().

type * TQPtrVector::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().

bool TQPtrCollection::autoDelete () const

Returns the setting of the auto-delete option. The default is FALSE.

See also setAutoDelete().

int TQPtrVector::bsearch ( const type * d ) const

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

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

Compares items using the virtual function compareItems().

See also sort() and find().

void TQPtrVector::clear () [virtual]

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

The vector becomes a null vector.

See also isNull().

Reimplemented from TQPtrCollection.

int TQPtrVector::compareItems ( TQPtrCollection::Item d1, TQPtrCollection::Item d2 ) [virtual protected]

This virtual function compares two list items.

Returns:

This function returns int rather than bool so that reimplementations can return one of three values and use it to sort by:

The sort() and bsearch() functions require compareItems() to be implemented as described here.

This function should not modify the vector because some const functions call compareItems().

uint TQPtrVector::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 TQPtrVector::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 TQPtrVector::count () const [virtual]

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

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

Reimplemented from TQPtrCollection.

type ** TQPtrVector::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 TQPtrVector::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, i.e. size is the same as the current size, or size is larger and the memory has successfully been allocated; otherwise returns FALSE.

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

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

Finds the first occurrence of item d in the vector using a 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.

Compares items using the virtual function compareItems().

Use the much faster bsearch() to search a sorted vector.

See also findRef() and bsearch().

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

Finds the first occurrence of the item pointer d in the vector using a 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.

Use the much faster bsearch() to search a sorted vector.

See also find() and bsearch().

bool TQPtrVector::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 TQPtrVector::isEmpty () const

Returns TRUE if the vector is empty; otherwise returns FALSE.

See also count().

bool TQPtrVector::isNull () const

Returns TRUE if the vector is null; otherwise returns FALSE.

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

See also size().

TQPtrVector<type> & TQPtrVector::operator= ( const TQPtrVector<type> & v )

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

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

See also clear().

bool TQPtrVector::operator== ( const TQPtrVector<type> & v ) const

Returns TRUE if this vector and v are equal; otherwise returns FALSE.

type * TQPtrVector::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().

TQDataStream & TQPtrVector::read ( TQDataStream & s, TQPtrCollection::Item & item ) [virtual protected]

Reads a vector item, item, from the stream s and returns a reference to the stream.

The default implementation sets item to 0.

See also write().

bool TQPtrVector::remove ( uint i )

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

Returns TRUE if i is within range; otherwise returns FALSE.

See also take() and at().

bool TQPtrVector::resize ( uint size )

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

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

Returns TRUE if successful, i.e. if the memory was successfully allocated; otherwise returns FALSE.

See also size() and isNull().

void TQPtrCollection::setAutoDelete ( bool enable )

Sets the collection to auto-delete its contents if enable is TRUE and to never delete them if enable is FALSE.

If auto-deleting is turned on, all the items in a collection are deleted when the collection itself is deleted. This is convenient if the collection has the only pointer to the items.

The default setting is FALSE, for safety. If you turn it on, be careful about copying the collection - you might find yourself with two collections deleting the same items.

Note that the auto-delete setting may also affect other functions in subclasses. For example, a subclass that has a remove() function will remove the item from its data structure, and if auto-delete is enabled, will also delete the item.

See also autoDelete().

Examples: scribble/scribble.cpp and table/bigtable/main.cpp.

uint TQPtrVector::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 TQPtrVector::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 * TQPtrVector::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.

Unlike remove(), this function does not call deleteItem() for the removed item.

See also remove() and at().

TQDataStream & TQPtrVector::write ( TQDataStream & s, TQPtrCollection::Item item ) const [virtual protected]

Writes a vector item, item, to the stream s and returns a reference to the stream.

The default implementation does nothing.

See also read().


This file is part of the TQt toolkit. Copyright © 1995-2007 Trolltech. All Rights Reserved.


Copyright © 2007 TrolltechTrademarks
TQt 3.3.8