1 /****************************************************************************
4 ** QVector class documentation
6 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
8 ** This file is part of the Qt GUI Toolkit.
10 ** This file may be distributed under the terms of the Q Public License
11 ** as defined by Trolltech AS of Norway and appearing in the file
12 ** LICENSE.QPL included in the packaging of this file.
14 ** This file may be distributed and/or modified under the terms of the
15 ** GNU General Public License version 2 as published by the Free Software
16 ** Foundation and appearing in the file LICENSE.GPL included in the
17 ** packaging of this file.
19 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
20 ** licenses may use this file in accordance with the Qt Commercial License
21 ** Agreement provided with the Software.
23 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
24 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
26 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
27 ** information about Qt Commercial License Agreements.
28 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
29 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
31 ** Contact info@trolltech.com if any conditions of this licensing are
34 **********************************************************************/
37 /*****************************************************************************
39 *****************************************************************************/
41 // BEING REVISED: ettrich
43 \class QVector qvector.h
45 \brief The QVector class is a template collection class that
46 provides a vector (array).
50 QVector is implemented as a template class. Define a template
51 instance QVector\<X\> to create a vector that contains pointers to
54 A vector is the same as an array. The main difference between
55 QVector and QArray is that QVector stores pointers to the elements,
56 while QArray stores the elements themselves (i.e. QArray is
59 Unless where otherwise stated, all functions that remove items from
60 the vector will also delete the element pointed to if auto-deletion
61 is enabled - see setAutoDelete(). By default, auto-deletion is
62 disabled. This behaviour can be changed in a subclass by
63 reimplementing the virtual method deleteItem().
65 Functions that compares items, e.g. find() and sort(), will do so
66 using the virtual function compareItems(). The default
67 implementation of this function will only compare the absolute
68 pointer values. Reimplement compareItems() in a subclass to get
69 searching and sorting based on the item contents.
71 \sa \link collection.html Collection Classes\endlink, QArray
75 \fn QVector::QVector()
77 Constructs a null vector.
83 \fn QVector::QVector( uint size )
85 Constructs an vector with room for \a size items. Makes a null
86 vector if \a size == 0.
88 All \a size positions in the vector are initialized to 0.
90 \sa size(), resize(), isNull()
94 \fn QVector::QVector( const QVector<type> &v )
96 Constructs a copy of \a v. Only the pointers are copied (i.e. shallow copy).
100 \fn QVector::~QVector()
102 Removes all items from the vector, and destroys the vector itself.
108 \fn QVector<type> &QVector::operator=( const QVector<type> &v )
110 Assigns \a v to this vector and returns a reference to this vector.
112 This vector is first cleared, then all the items from \a v is copied
113 into this vector. Only the pointers are copied (i.e. shallow copy).
119 \fn type **QVector::data() const
120 Returns a pointer to the actual vector data, which is an array of type*.
122 The vector is a null vector if data() == 0 (null pointer).
128 \fn uint QVector::size() const
130 Returns the size of the vector, i.e. the number of vector
131 positions. This is also the maximum number of items the vector can
134 The vector is a null vector if size() == 0.
136 \sa isNull(), resize(), count()
140 \fn uint QVector::count() const
142 Returns the number of items in the vector. The vector is empty if
145 \sa isEmpty(), size()
149 \fn bool QVector::isEmpty() const
151 Returns TRUE if the vector is empty, i.e. count() == 0, otherwise FALSE.
157 \fn bool QVector::isNull() const
159 Returns TRUE if the vector is null, otherwise FALSE.
161 A null vector has size() == 0 and data() == 0.
167 \fn bool QVector::resize( uint size )
168 Resizes (expands or shrinks) the vector to \a size elements. The array
169 becomes a null array if \a size == 0.
171 Any items in position \a size or beyond in the vector are removed.
172 New positions are initialized 0.
174 Returns TRUE if successful, or FALSE if the memory cannot be allocated.
180 \fn bool QVector::insert( uint i, const type *d )
182 Sets position \a i in the vector to contain the item \a d. \a i must
183 be less than size(). Any previous element in position \a i is removed.
189 \fn bool QVector::remove( uint i )
191 Removes the item at position \a i in the vector, if there is one.
192 \a i must be less than size().
194 Returns TRUE unless \a i is out of range.
200 \fn type* QVector::take( uint i )
202 Returns the item at position \a i in the vector, and removes that
203 item from the vector. \a i must be less than size(). If there is no
204 item at position \a i, 0 is returned.
206 In contrast to remove(), this function does \e not call deleteItem()
207 for the removed item.
213 \fn void QVector::clear()
215 Removes all items from the vector, and destroys the vector
218 The vector becomes a null vector.
224 \fn bool QVector::fill( const type *d, int size )
226 Inserts item \a d in all positions in the vector. Any existing items
227 are removed. If \a d is 0, the vector becomes empty.
229 If \a size >= 0, the vector is first resized to \a size. By default,
232 Returns TRUE if successful, or FALSE if the memory cannot be allocated
233 (only if a resize has been requested).
235 \sa resize(), insert(), isEmpty()
239 \fn void QVector::sort()
241 Sorts the items in ascending order. Any empty positions will be put
244 Compares items using the virtual function compareItems().
250 \fn int QVector::bsearch( const type* d ) const
252 In a sorted array, finds the first occurrence of \a d using binary
253 search. For a sorted array, this is generally much faster than
254 find(), which does a linear search.
256 Returns the position of \a d, or -1 if \a d could not be found. \a d
259 Compares items using the virtual function compareItems().
266 \fn int QVector::findRef( const type *d, uint i ) const
268 Finds the first occurrence of the item pointer \a d in the vector,
269 using linear search. The search starts at position \a i, which must
270 be less than size(). \a i is by default 0; i.e. the search starts at
271 the start of the vector.
273 Returns the position of \a d, or -1 if \a d could not be found.
275 This function does \e not use compareItems() to compare items.
277 \sa find(), bsearch()
281 \fn int QVector::find( const type *d, uint i ) const
283 Finds the first occurrence of item \a d in the vector, using linear
284 search. The search starts at position \a i, which must be less than
285 size(). \a i is by default 0; i.e. the search starts at the start of
288 Returns the position of \e v, or -1 if \e v could not be found.
290 Compares items using the virtual function compareItems().
292 \sa findRef(), bsearch()
297 \fn uint QVector::containsRef( const type *d ) const
299 Returns the number of occurrences of the item pointer \a d in the
302 This function does \e not use compareItems() to compare items.
308 \fn uint QVector::contains( const type *d ) const
310 Returns the number of occurrences of item \a d in the vector.
312 Compares items using the virtual function compareItems().
318 \fn type *QVector::operator[]( int i ) const
320 Returns the item at position \a i, or 0 if there is no item at
321 that position. \a i must be less than size().
323 Equivalent to at( \a i ).
329 \fn type *QVector::at( uint i ) const
331 Returns the item at position \a i, or 0 if there is no item at
332 that position. \a i must be less than size().
337 \fn void QVector::toList( QGList *list ) const
339 Copies all items in this vector to the list \a list. First, \a list
340 is cleared, then all items are appended to \a list.
342 \sa QList, QStack, QQueue