source: trunk/doc/html/qptrvector.html@ 203

Last change on this file since 203 was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 20.1 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/qptrvector.doc:41 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QPtrVector Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QPtrVector Class Reference</h1>
33
34<p>The QPtrVector class is a template collection class that
35provides a vector (array).
36<a href="#details">More...</a>
37<p><tt>#include &lt;<a href="qptrvector-h.html">qptrvector.h</a>&gt;</tt>
38<p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.
39<p><a href="qptrvector-members.html">List of all member functions.</a>
40<h2>Public Members</h2>
41<ul>
42<li class=fn><a href="#QPtrVector"><b>QPtrVector</b></a> ()</li>
43<li class=fn><a href="#QPtrVector-2"><b>QPtrVector</b></a> ( uint&nbsp;size )</li>
44<li class=fn><a href="#QPtrVector-3"><b>QPtrVector</b></a> ( const&nbsp;QPtrVector&lt;type&gt;&nbsp;&amp;&nbsp;v )</li>
45<li class=fn><a href="#~QPtrVector"><b>~QPtrVector</b></a> ()</li>
46<li class=fn>QPtrVector&lt;type&gt; &amp; <a href="#operator-eq"><b>operator=</b></a> ( const&nbsp;QPtrVector&lt;type&gt;&nbsp;&amp;&nbsp;v )</li>
47<li class=fn>bool <a href="#operator-eq-eq"><b>operator==</b></a> ( const&nbsp;QPtrVector&lt;type&gt;&nbsp;&amp;&nbsp;v ) const</li>
48<li class=fn>type ** <a href="#data"><b>data</b></a> () const</li>
49<li class=fn>uint <a href="#size"><b>size</b></a> () const</li>
50<li class=fn>virtual uint <a href="#count"><b>count</b></a> () const</li>
51<li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
52<li class=fn>bool <a href="#isNull"><b>isNull</b></a> () const</li>
53<li class=fn>bool <a href="#resize"><b>resize</b></a> ( uint&nbsp;size )</li>
54<li class=fn>bool <a href="#insert"><b>insert</b></a> ( uint&nbsp;i, const&nbsp;type&nbsp;*&nbsp;d )</li>
55<li class=fn>bool <a href="#remove"><b>remove</b></a> ( uint&nbsp;i )</li>
56<li class=fn>type * <a href="#take"><b>take</b></a> ( uint&nbsp;i )</li>
57<li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
58<li class=fn>bool <a href="#fill"><b>fill</b></a> ( const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;size = -1 )</li>
59<li class=fn>void <a href="#sort"><b>sort</b></a> ()</li>
60<li class=fn>int <a href="#bsearch"><b>bsearch</b></a> ( const&nbsp;type&nbsp;*&nbsp;d ) const</li>
61<li class=fn>int <a href="#findRef"><b>findRef</b></a> ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const</li>
62<li class=fn>int <a href="#find"><b>find</b></a> ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const</li>
63<li class=fn>uint <a href="#containsRef"><b>containsRef</b></a> ( const&nbsp;type&nbsp;*&nbsp;d ) const</li>
64<li class=fn>uint <a href="#contains"><b>contains</b></a> ( const&nbsp;type&nbsp;*&nbsp;d ) const</li>
65<li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( int&nbsp;i ) const</li>
66<li class=fn>type * <a href="#at"><b>at</b></a> ( uint&nbsp;i ) const</li>
67</ul>
68<h2>Important Inherited Members</h2>
69<ul>
70<li class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</li>
71<li class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool&nbsp;enable )</li>
72</ul>
73<h2>Protected Members</h2>
74<ul>
75<li class=fn>virtual int <a href="#compareItems"><b>compareItems</b></a> ( QPtrCollection::Item&nbsp;d1, QPtrCollection::Item&nbsp;d2 )</li>
76<li class=fn>virtual QDataStream &amp; <a href="#read"><b>read</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QPtrCollection::Item&nbsp;&amp;&nbsp;item )</li>
77<li class=fn>virtual QDataStream &amp; <a href="#write"><b>write</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QPtrCollection::Item&nbsp;item ) const</li>
78</ul>
79<hr><a name="details"></a><h2>Detailed Description</h2>
80
81
82The QPtrVector class is a template collection class that
83provides a vector (array).
84<p>
85<p>
86<p> <a href="qvaluevector.html">QValueVector</a> is an STL-compatible alternative to this class.
87<p> QPtrVector is implemented as a template class. Defines a template
88instance QPtrVector&lt;X&gt; to create a vector that contains pointers
89to X (X*).
90<p> A vector is the same as an array. The main difference between
91QPtrVector and <a href="qmemarray.html">QMemArray</a> is that QPtrVector stores pointers to the
92elements, whereas QMemArray stores the elements themselves (i.e.
93QMemArray is value-based and QPtrVector is pointer-based).
94<p> Items are added to the vector using <a href="#insert">insert</a>() or <a href="#fill">fill</a>(). Items are
95removed with <a href="#remove">remove</a>(). You can get a pointer to an item at a
96particular index position using <a href="#at">at</a>().
97<p> Unless otherwise stated, all functions that remove items from the
98vector will also delete the element pointed to if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled. By default,
99auto-deletion is disabled; see <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(). This behaviour can
100be changed in a subclass by reimplementing the virtual function
101<a href="qptrcollection.html#deleteItem">deleteItem</a>().
102<p> Functions that compare items (<a href="#find">find</a>() and <a href="#sort">sort</a>() for example) will
103do so using the virtual function <a href="#compareItems">compareItems</a>(). The default
104implementation of this function only compares the pointer values.
105Reimplement compareItems() in a subclass to get searching and
106sorting based on the item contents. You can perform a linear
107search for a pointer in the vector using <a href="#findRef">findRef</a>(), or a binary
108search (of a sorted vector) using <a href="#bsearch">bsearch</a>(). You can count the
109number of times an item appears in the vector with <a href="#contains">contains</a>() or
110<a href="#containsRef">containsRef</a>().
111<p> <p>See also <a href="qmemarray.html">QMemArray</a> and <a href="tools.html">Non-GUI Classes</a>.
112
113<hr><h2>Member Function Documentation</h2>
114<h3 class=fn><a name="QPtrVector"></a>QPtrVector::QPtrVector ()
115</h3>
116
117<p> Constructs a null vector.
118<p> <p>See also <a href="#isNull">isNull</a>().
119
120<h3 class=fn><a name="QPtrVector-2"></a>QPtrVector::QPtrVector ( uint&nbsp;size )
121</h3>
122
123<p> Constructs an vector with room for <em>size</em> items. Makes a null
124vector if <em>size</em> == 0.
125<p> All <em>size</em> positions in the vector are initialized to 0.
126<p> <p>See also <a href="#size">size</a>(), <a href="#resize">resize</a>(), and <a href="#isNull">isNull</a>().
127
128<h3 class=fn><a name="QPtrVector-3"></a>QPtrVector::QPtrVector ( const&nbsp;<a href="qptrvector.html">QPtrVector</a>&lt;type&gt;&nbsp;&amp;&nbsp;v )
129</h3>
130
131<p> Constructs a copy of <em>v</em>. Only the pointers are copied (i.e.
132<a href="shclass.html#shallow-copy">shallow copy</a>).
133
134<h3 class=fn><a name="~QPtrVector"></a>QPtrVector::~QPtrVector ()
135</h3>
136
137<p> Removes all items from the vector, and destroys the vector itself.
138<p> <p>See also <a href="#clear">clear</a>().
139
140<h3 class=fn>type * <a name="at"></a>QPtrVector::at ( uint&nbsp;i ) const
141</h3>
142
143<p> Returns the item at position <em>i</em>, or 0 if there is no item at
144that position. <em>i</em> must be less than <a href="#size">size</a>().
145
146<h3 class=fn>bool <a name="autoDelete"></a>QPtrCollection::autoDelete () const
147</h3>
148
149<p> Returns the setting of the auto-delete option. The default is FALSE.
150<p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
151
152<h3 class=fn>int <a name="bsearch"></a>QPtrVector::bsearch ( const&nbsp;type&nbsp;*&nbsp;d ) const
153</h3>
154
155<p> In a sorted array, finds the first occurrence of <em>d</em> using a
156binary search. For a sorted array, this is generally much faster
157than <a href="#find">find</a>(), which performs a linear search.
158<p> Returns the position of <em>d</em>, or -1 if <em>d</em> could not be found. <em>d</em> must not be 0.
159<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
160<p> <p>See also <a href="#sort">sort</a>() and <a href="#find">find</a>().
161
162<h3 class=fn>void <a name="clear"></a>QPtrVector::clear ()<tt> [virtual]</tt>
163</h3>
164
165<p> Removes all items from the vector, and destroys the vector itself.
166<p> The vector becomes a null vector.
167<p> <p>See also <a href="#isNull">isNull</a>().
168
169<p>Reimplemented from <a href="qptrcollection.html#clear">QPtrCollection</a>.
170<h3 class=fn>int <a name="compareItems"></a>QPtrVector::compareItems ( <a href="qptrcollection.html#Item">QPtrCollection::Item</a>&nbsp;d1, <a href="qptrcollection.html#Item">QPtrCollection::Item</a>&nbsp;d2 )<tt> [virtual protected]</tt>
171</h3>
172
173<p> This virtual function compares two list items.
174<p> Returns:
175<ul>
176<li> zero if <em>d1</em> == <em>d2</em>
177<li> nonzero if <em>d1</em> != <em>d2</em>
178</ul>
179<p> This function returns <em>int</em> rather than <em>bool</em> so that
180reimplementations can return one of three values and use it to
181sort by:
182<ul>
183<li> 0 if <em>d1</em> == <em>d2</em>
184<li> &gt; 0 (positive integer) if <em>d1</em> &gt; <em>d2</em>
185<li> &lt; 0 (negative integer) if <em>d1</em> &lt; <em>d2</em>
186</ul>
187<p> The <a href="#sort">sort</a>() and <a href="#bsearch">bsearch</a>() functions require <a href="#compareItems">compareItems</a>() to be
188implemented as described here.
189<p> This function should not modify the vector because some const
190functions call compareItems().
191
192<h3 class=fn>uint <a name="contains"></a>QPtrVector::contains ( const&nbsp;type&nbsp;*&nbsp;d ) const
193</h3>
194
195<p> Returns the number of occurrences of item <em>d</em> in the vector.
196<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
197<p> <p>See also <a href="#containsRef">containsRef</a>().
198
199<h3 class=fn>uint <a name="containsRef"></a>QPtrVector::containsRef ( const&nbsp;type&nbsp;*&nbsp;d ) const
200</h3>
201
202<p> Returns the number of occurrences of the item pointer <em>d</em> in the
203vector.
204<p> This function does <em>not</em> use <a href="#compareItems">compareItems</a>() to compare items.
205<p> <p>See also <a href="#findRef">findRef</a>().
206
207<h3 class=fn>uint <a name="count"></a>QPtrVector::count () const<tt> [virtual]</tt>
208</h3>
209
210<p> Returns the number of items in the vector. The vector is empty if
211<a href="#count">count</a>() == 0.
212<p> <p>See also <a href="#isEmpty">isEmpty</a>(), <a href="#size">size</a>(), and <a href="#isNull">isNull</a>().
213
214<p>Reimplemented from <a href="qptrcollection.html#count">QPtrCollection</a>.
215<h3 class=fn>type ** <a name="data"></a>QPtrVector::data () const
216</h3>
217
218<p> Returns a pointer to the actual vector data, which is an array of
219type*.
220<p> The vector is a null vector if <a href="#data">data</a>() == 0 (null pointer).
221<p> <p>See also <a href="#isNull">isNull</a>().
222
223<h3 class=fn>bool <a name="fill"></a>QPtrVector::fill ( const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;size = -1 )
224</h3>
225
226<p> Inserts item <em>d</em> in all positions in the vector. Any existing
227items are removed. If <em>d</em> is 0, the vector becomes empty.
228<p> If <em>size</em> >= 0, the vector is first resized to <em>size</em>. By
229default, <em>size</em> is -1.
230<p> Returns TRUE if successful, i.e. <em>size</em> is the same as the
231current size, or <em>size</em> is larger and the memory has successfully
232been allocated; otherwise returns FALSE.
233<p> <p>See also <a href="#resize">resize</a>(), <a href="#insert">insert</a>(), and <a href="#isEmpty">isEmpty</a>().
234
235<h3 class=fn>int <a name="find"></a>QPtrVector::find ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const
236</h3>
237
238<p> Finds the first occurrence of item <em>d</em> in the vector using a
239linear search. The search starts at position <em>i</em>, which must be
240less than <a href="#size">size</a>(). <em>i</em> is by default 0; i.e. the search starts at
241the start of the vector.
242<p> Returns the position of <em>d</em>, or -1 if <em>d</em> could not be found.
243<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
244<p> Use the much faster <a href="#bsearch">bsearch</a>() to search a sorted vector.
245<p> <p>See also <a href="#findRef">findRef</a>() and <a href="#bsearch">bsearch</a>().
246
247<h3 class=fn>int <a name="findRef"></a>QPtrVector::findRef ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const
248</h3>
249
250<p> Finds the first occurrence of the item pointer <em>d</em> in the vector
251using a linear search. The search starts at position <em>i</em>, which
252must be less than <a href="#size">size</a>(). <em>i</em> is by default 0; i.e. the search
253starts at the start of the vector.
254<p> Returns the position of <em>d</em>, or -1 if <em>d</em> could not be found.
255<p> This function does <em>not</em> use <a href="#compareItems">compareItems</a>() to compare items.
256<p> Use the much faster <a href="#bsearch">bsearch</a>() to search a sorted vector.
257<p> <p>See also <a href="#find">find</a>() and <a href="#bsearch">bsearch</a>().
258
259<h3 class=fn>bool <a name="insert"></a>QPtrVector::insert ( uint&nbsp;i, const&nbsp;type&nbsp;*&nbsp;d )
260</h3>
261
262<p> Sets position <em>i</em> in the vector to contain the item <em>d</em>. <em>i</em>
263must be less than <a href="#size">size</a>(). Any previous element in position <em>i</em> is
264removed.
265<p> <p>See also <a href="#at">at</a>().
266
267<h3 class=fn>bool <a name="isEmpty"></a>QPtrVector::isEmpty () const
268</h3>
269
270<p> Returns TRUE if the vector is empty; otherwise returns FALSE.
271<p> <p>See also <a href="#count">count</a>().
272
273<h3 class=fn>bool <a name="isNull"></a>QPtrVector::isNull () const
274</h3>
275
276<p> Returns TRUE if the vector is null; otherwise returns FALSE.
277<p> A null vector has <a href="#size">size</a>() == 0 and <a href="#data">data</a>() == 0.
278<p> <p>See also <a href="#size">size</a>().
279
280<h3 class=fn><a href="qptrvector.html">QPtrVector</a>&lt;type&gt;&nbsp;&amp; <a name="operator-eq"></a>QPtrVector::operator= ( const&nbsp;<a href="qptrvector.html">QPtrVector</a>&lt;type&gt;&nbsp;&amp;&nbsp;v )
281</h3>
282
283<p> Assigns <em>v</em> to this vector and returns a reference to this
284vector.
285<p> This vector is first cleared and then all the items from <em>v</em> are
286copied into the vector. Only the pointers are copied (i.e. <a href="shclass.html#shallow-copy">shallow copy</a>).
287<p> <p>See also <a href="#clear">clear</a>().
288
289<h3 class=fn>bool <a name="operator-eq-eq"></a>QPtrVector::operator== ( const&nbsp;<a href="qptrvector.html">QPtrVector</a>&lt;type&gt;&nbsp;&amp;&nbsp;v ) const
290</h3>
291
292<p> Returns TRUE if this vector and <em>v</em> are equal; otherwise returns
293FALSE.
294
295<h3 class=fn>type * <a name="operator[]"></a>QPtrVector::operator[] ( int&nbsp;i ) const
296</h3>
297
298<p> Returns the item at position <em>i</em>, or 0 if there is no item at
299that position. <em>i</em> must be less than <a href="#size">size</a>().
300<p> Equivalent to <a href="#at">at</a>( <em>i</em> ).
301<p> <p>See also <a href="#at">at</a>().
302
303<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="read"></a>QPtrVector::read ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, <a href="qptrcollection.html#Item">QPtrCollection::Item</a>&nbsp;&amp;&nbsp;item )<tt> [virtual protected]</tt>
304</h3>
305
306<p> Reads a vector item, <em>item</em>, from the stream <em>s</em> and returns a
307reference to the stream.
308<p> The default implementation sets <em>item</em> to 0.
309<p> <p>See also <a href="#write">write</a>().
310
311<h3 class=fn>bool <a name="remove"></a>QPtrVector::remove ( uint&nbsp;i )
312</h3>
313
314<p> Removes the item at position <em>i</em> in the vector, if there is one.
315<em>i</em> must be less than <a href="#size">size</a>().
316<p> Returns TRUE if <em>i</em> is within range; otherwise returns FALSE.
317<p> <p>See also <a href="#take">take</a>() and <a href="#at">at</a>().
318
319<h3 class=fn>bool <a name="resize"></a>QPtrVector::resize ( uint&nbsp;size )
320</h3>
321
322<p> Resizes (expands or shrinks) the vector to <em>size</em> elements. The
323vector becomes a null vector if <em>size</em> == 0.
324<p> Any items at position <em>size</em> or beyond in the vector are removed.
325New positions are initialized to 0.
326<p> Returns TRUE if successful, i.e. if the memory was successfully
327allocated; otherwise returns FALSE.
328<p> <p>See also <a href="#size">size</a>() and <a href="#isNull">isNull</a>().
329
330<h3 class=fn>void <a name="setAutoDelete"></a>QPtrCollection::setAutoDelete ( bool&nbsp;enable )
331</h3>
332
333<p> Sets the collection to auto-delete its contents if <em>enable</em> is
334TRUE and to never delete them if <em>enable</em> is FALSE.
335<p> If auto-deleting is turned on, all the items in a collection are
336deleted when the collection itself is deleted. This is convenient
337if the collection has the only pointer to the items.
338<p> The default setting is FALSE, for safety. If you turn it on, be
339careful about copying the collection - you might find yourself
340with two collections deleting the same items.
341<p> Note that the auto-delete setting may also affect other functions
342in subclasses. For example, a subclass that has a <a href="#remove">remove</a>()
343function will remove the item from its data structure, and if
344auto-delete is enabled, will also delete the item.
345<p> <p>See also <a href="qptrcollection.html#autoDelete">autoDelete</a>().
346
347<p>Examples: <a href="grapher-nsplugin-example.html#x2769">grapher/grapher.cpp</a>, <a href="scribble-example.html#x924">scribble/scribble.cpp</a>, and <a href="bigtable-example.html#x1291">table/bigtable/main.cpp</a>.
348<h3 class=fn>uint <a name="size"></a>QPtrVector::size () const
349</h3>
350
351<p> Returns the size of the vector, i.e. the number of vector
352positions. This is also the maximum number of items the vector can
353hold.
354<p> The vector is a null vector if <a href="#size">size</a>() == 0.
355<p> <p>See also <a href="#isNull">isNull</a>(), <a href="#resize">resize</a>(), and <a href="#count">count</a>().
356
357<h3 class=fn>void <a name="sort"></a>QPtrVector::sort ()
358</h3>
359
360<p> Sorts the items in ascending order. Any empty positions will be
361put last.
362<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
363<p> <p>See also <a href="#bsearch">bsearch</a>().
364
365<h3 class=fn>type * <a name="take"></a>QPtrVector::take ( uint&nbsp;i )
366</h3>
367
368<p> Returns the item at position <em>i</em> in the vector, and removes that
369item from the vector. <em>i</em> must be less than <a href="#size">size</a>(). If there is
370no item at position <em>i</em>, 0 is returned.
371<p> Unlike <a href="#remove">remove</a>(), this function does <em>not</em> call <a href="qptrcollection.html#deleteItem">deleteItem</a>() for
372the removed item.
373<p> <p>See also <a href="#remove">remove</a>() and <a href="#at">at</a>().
374
375<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="write"></a>QPtrVector::write ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, <a href="qptrcollection.html#Item">QPtrCollection::Item</a>&nbsp;item ) const<tt> [virtual protected]</tt>
376</h3>
377
378<p> Writes a vector item, <em>item</em>, to the stream <em>s</em> and returns a
379reference to the stream.
380<p> The default implementation does nothing.
381<p> <p>See also <a href="#read">read</a>().
382
383<!-- eof -->
384<hr><p>
385This file is part of the <a href="index.html">Qt toolkit</a>.
386Copyright &copy; 1995-2007
387<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
388<table width=100% cellspacing=0 border=0><tr>
389<td>Copyright &copy; 2007
390<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
391<td align=right><div align=right>Qt 3.3.8</div>
392</table></div></address></body>
393</html>
Note: See TracBrowser for help on using the repository browser.