You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
qt3/doc/html/qptrvector.html

394 lines
20 KiB

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/qptrvector.doc:41 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QPtrVector Class</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
<a href="index.html">
<font color="#004faf">Home</font></a>
| <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
| <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
| <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
| <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
| <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<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>
<p>The QPtrVector class is a template collection class that
provides a vector (array).
<a href="#details">More...</a>
<p><tt>#include &lt;<a href="qptrvector-h.html">qptrvector.h</a>&gt;</tt>
<p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.
<p><a href="qptrvector-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li class=fn><a href="#QPtrVector"><b>QPtrVector</b></a> ()</li>
<li class=fn><a href="#QPtrVector-2"><b>QPtrVector</b></a> ( uint&nbsp;size )</li>
<li class=fn><a href="#QPtrVector-3"><b>QPtrVector</b></a> ( const&nbsp;QPtrVector&lt;type&gt;&nbsp;&amp;&nbsp;v )</li>
<li class=fn><a href="#~QPtrVector"><b>~QPtrVector</b></a> ()</li>
<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>
<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>
<li class=fn>type ** <a href="#data"><b>data</b></a> () const</li>
<li class=fn>uint <a href="#size"><b>size</b></a> () const</li>
<li class=fn>virtual uint <a href="#count"><b>count</b></a> () const</li>
<li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
<li class=fn>bool <a href="#isNull"><b>isNull</b></a> () const</li>
<li class=fn>bool <a href="#resize"><b>resize</b></a> ( uint&nbsp;size )</li>
<li class=fn>bool <a href="#insert"><b>insert</b></a> ( uint&nbsp;i, const&nbsp;type&nbsp;*&nbsp;d )</li>
<li class=fn>bool <a href="#remove"><b>remove</b></a> ( uint&nbsp;i )</li>
<li class=fn>type * <a href="#take"><b>take</b></a> ( uint&nbsp;i )</li>
<li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
<li class=fn>bool <a href="#fill"><b>fill</b></a> ( const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;size = -1 )</li>
<li class=fn>void <a href="#sort"><b>sort</b></a> ()</li>
<li class=fn>int <a href="#bsearch"><b>bsearch</b></a> ( const&nbsp;type&nbsp;*&nbsp;d ) const</li>
<li class=fn>int <a href="#findRef"><b>findRef</b></a> ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const</li>
<li class=fn>int <a href="#find"><b>find</b></a> ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const</li>
<li class=fn>uint <a href="#containsRef"><b>containsRef</b></a> ( const&nbsp;type&nbsp;*&nbsp;d ) const</li>
<li class=fn>uint <a href="#contains"><b>contains</b></a> ( const&nbsp;type&nbsp;*&nbsp;d ) const</li>
<li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( int&nbsp;i ) const</li>
<li class=fn>type * <a href="#at"><b>at</b></a> ( uint&nbsp;i ) const</li>
</ul>
<h2>Important Inherited Members</h2>
<ul>
<li class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</li>
<li class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool&nbsp;enable )</li>
</ul>
<h2>Protected Members</h2>
<ul>
<li class=fn>virtual int <a href="#compareItems"><b>compareItems</b></a> ( QPtrCollection::Item&nbsp;d1, QPtrCollection::Item&nbsp;d2 )</li>
<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>
<li class=fn>virtual QDataStream &amp; <a href="#write"><b>write</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QPtrCollection::Item&nbsp;item ) const</li>
</ul>
<hr><a name="details"></a><h2>Detailed Description</h2>
The QPtrVector class is a template collection class that
provides a vector (array).
<p>
<p>
<p> <a href="qvaluevector.html">QValueVector</a> is an STL-compatible alternative to this class.
<p> QPtrVector is implemented as a template class. Defines a template
instance QPtrVector&lt;X&gt; to create a vector that contains pointers
to X (X*).
<p> A vector is the same as an array. The main difference between
QPtrVector and <a href="qmemarray.html">QMemArray</a> is that QPtrVector stores pointers to the
elements, whereas QMemArray stores the elements themselves (i.e.
QMemArray is value-based and QPtrVector is pointer-based).
<p> Items are added to the vector using <a href="#insert">insert</a>() or <a href="#fill">fill</a>(). Items are
removed with <a href="#remove">remove</a>(). You can get a pointer to an item at a
particular index position using <a href="#at">at</a>().
<p> Unless otherwise stated, all functions that remove items from the
vector will also delete the element pointed to if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled. By default,
auto-deletion is disabled; see <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(). This behaviour can
be changed in a subclass by reimplementing the virtual function
<a href="qptrcollection.html#deleteItem">deleteItem</a>().
<p> Functions that compare items (<a href="#find">find</a>() and <a href="#sort">sort</a>() for example) will
do so using the virtual function <a href="#compareItems">compareItems</a>(). 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 <a href="#findRef">findRef</a>(), or a binary
search (of a sorted vector) using <a href="#bsearch">bsearch</a>(). You can count the
number of times an item appears in the vector with <a href="#contains">contains</a>() or
<a href="#containsRef">containsRef</a>().
<p> <p>See also <a href="qmemarray.html">QMemArray</a> and <a href="tools.html">Non-GUI Classes</a>.
<hr><h2>Member Function Documentation</h2>
<h3 class=fn><a name="QPtrVector"></a>QPtrVector::QPtrVector ()
</h3>
<p> Constructs a null vector.
<p> <p>See also <a href="#isNull">isNull</a>().
<h3 class=fn><a name="QPtrVector-2"></a>QPtrVector::QPtrVector ( uint&nbsp;size )
</h3>
<p> Constructs an vector with room for <em>size</em> items. Makes a null
vector if <em>size</em> == 0.
<p> All <em>size</em> positions in the vector are initialized to 0.
<p> <p>See also <a href="#size">size</a>(), <a href="#resize">resize</a>(), and <a href="#isNull">isNull</a>().
<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 )
</h3>
<p> Constructs a copy of <em>v</em>. Only the pointers are copied (i.e.
<a href="shclass.html#shallow-copy">shallow copy</a>).
<h3 class=fn><a name="~QPtrVector"></a>QPtrVector::~QPtrVector ()
</h3>
<p> Removes all items from the vector, and destroys the vector itself.
<p> <p>See also <a href="#clear">clear</a>().
<h3 class=fn>type * <a name="at"></a>QPtrVector::at ( uint&nbsp;i ) const
</h3>
<p> Returns the item at position <em>i</em>, or 0 if there is no item at
that position. <em>i</em> must be less than <a href="#size">size</a>().
<h3 class=fn>bool <a name="autoDelete"></a>QPtrCollection::autoDelete () const
</h3>
<p> Returns the setting of the auto-delete option. The default is FALSE.
<p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
<h3 class=fn>int <a name="bsearch"></a>QPtrVector::bsearch ( const&nbsp;type&nbsp;*&nbsp;d ) const
</h3>
<p> In a sorted array, finds the first occurrence of <em>d</em> using a
binary search. For a sorted array, this is generally much faster
than <a href="#find">find</a>(), which performs a linear search.
<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.
<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
<p> <p>See also <a href="#sort">sort</a>() and <a href="#find">find</a>().
<h3 class=fn>void <a name="clear"></a>QPtrVector::clear ()<tt> [virtual]</tt>
</h3>
<p> Removes all items from the vector, and destroys the vector itself.
<p> The vector becomes a null vector.
<p> <p>See also <a href="#isNull">isNull</a>().
<p>Reimplemented from <a href="qptrcollection.html#clear">QPtrCollection</a>.
<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>
</h3>
<p> This virtual function compares two list items.
<p> Returns:
<ul>
<li> zero if <em>d1</em> == <em>d2</em>
<li> nonzero if <em>d1</em> != <em>d2</em>
</ul>
<p> This function returns <em>int</em> rather than <em>bool</em> so that
reimplementations can return one of three values and use it to
sort by:
<ul>
<li> 0 if <em>d1</em> == <em>d2</em>
<li> &gt; 0 (positive integer) if <em>d1</em> &gt; <em>d2</em>
<li> &lt; 0 (negative integer) if <em>d1</em> &lt; <em>d2</em>
</ul>
<p> The <a href="#sort">sort</a>() and <a href="#bsearch">bsearch</a>() functions require <a href="#compareItems">compareItems</a>() to be
implemented as described here.
<p> This function should not modify the vector because some const
functions call compareItems().
<h3 class=fn>uint <a name="contains"></a>QPtrVector::contains ( const&nbsp;type&nbsp;*&nbsp;d ) const
</h3>
<p> Returns the number of occurrences of item <em>d</em> in the vector.
<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
<p> <p>See also <a href="#containsRef">containsRef</a>().
<h3 class=fn>uint <a name="containsRef"></a>QPtrVector::containsRef ( const&nbsp;type&nbsp;*&nbsp;d ) const
</h3>
<p> Returns the number of occurrences of the item pointer <em>d</em> in the
vector.
<p> This function does <em>not</em> use <a href="#compareItems">compareItems</a>() to compare items.
<p> <p>See also <a href="#findRef">findRef</a>().
<h3 class=fn>uint <a name="count"></a>QPtrVector::count () const<tt> [virtual]</tt>
</h3>
<p> Returns the number of items in the vector. The vector is empty if
<a href="#count">count</a>() == 0.
<p> <p>See also <a href="#isEmpty">isEmpty</a>(), <a href="#size">size</a>(), and <a href="#isNull">isNull</a>().
<p>Reimplemented from <a href="qptrcollection.html#count">QPtrCollection</a>.
<h3 class=fn>type ** <a name="data"></a>QPtrVector::data () const
</h3>
<p> Returns a pointer to the actual vector data, which is an array of
type*.
<p> The vector is a null vector if <a href="#data">data</a>() == 0 (null pointer).
<p> <p>See also <a href="#isNull">isNull</a>().
<h3 class=fn>bool <a name="fill"></a>QPtrVector::fill ( const&nbsp;type&nbsp;*&nbsp;d, int&nbsp;size = -1 )
</h3>
<p> Inserts item <em>d</em> in all positions in the vector. Any existing
items are removed. If <em>d</em> is 0, the vector becomes empty.
<p> If <em>size</em> >= 0, the vector is first resized to <em>size</em>. By
default, <em>size</em> is -1.
<p> Returns TRUE if successful, i.e. <em>size</em> is the same as the
current size, or <em>size</em> is larger and the memory has successfully
been allocated; otherwise returns FALSE.
<p> <p>See also <a href="#resize">resize</a>(), <a href="#insert">insert</a>(), and <a href="#isEmpty">isEmpty</a>().
<h3 class=fn>int <a name="find"></a>QPtrVector::find ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const
</h3>
<p> Finds the first occurrence of item <em>d</em> in the vector using a
linear search. The search starts at position <em>i</em>, which must be
less than <a href="#size">size</a>(). <em>i</em> is by default 0; i.e. the search starts at
the start of the vector.
<p> Returns the position of <em>d</em>, or -1 if <em>d</em> could not be found.
<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
<p> Use the much faster <a href="#bsearch">bsearch</a>() to search a sorted vector.
<p> <p>See also <a href="#findRef">findRef</a>() and <a href="#bsearch">bsearch</a>().
<h3 class=fn>int <a name="findRef"></a>QPtrVector::findRef ( const&nbsp;type&nbsp;*&nbsp;d, uint&nbsp;i = 0 ) const
</h3>
<p> Finds the first occurrence of the item pointer <em>d</em> in the vector
using a linear search. The search starts at position <em>i</em>, which
must be less than <a href="#size">size</a>(). <em>i</em> is by default 0; i.e. the search
starts at the start of the vector.
<p> Returns the position of <em>d</em>, or -1 if <em>d</em> could not be found.
<p> This function does <em>not</em> use <a href="#compareItems">compareItems</a>() to compare items.
<p> Use the much faster <a href="#bsearch">bsearch</a>() to search a sorted vector.
<p> <p>See also <a href="#find">find</a>() and <a href="#bsearch">bsearch</a>().
<h3 class=fn>bool <a name="insert"></a>QPtrVector::insert ( uint&nbsp;i, const&nbsp;type&nbsp;*&nbsp;d )
</h3>
<p> Sets position <em>i</em> in the vector to contain the item <em>d</em>. <em>i</em>
must be less than <a href="#size">size</a>(). Any previous element in position <em>i</em> is
removed.
<p> <p>See also <a href="#at">at</a>().
<h3 class=fn>bool <a name="isEmpty"></a>QPtrVector::isEmpty () const
</h3>
<p> Returns TRUE if the vector is empty; otherwise returns FALSE.
<p> <p>See also <a href="#count">count</a>().
<h3 class=fn>bool <a name="isNull"></a>QPtrVector::isNull () const
</h3>
<p> Returns TRUE if the vector is null; otherwise returns FALSE.
<p> A null vector has <a href="#size">size</a>() == 0 and <a href="#data">data</a>() == 0.
<p> <p>See also <a href="#size">size</a>().
<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 )
</h3>
<p> Assigns <em>v</em> to this vector and returns a reference to this
vector.
<p> This vector is first cleared and then all the items from <em>v</em> are
copied into the vector. Only the pointers are copied (i.e. <a href="shclass.html#shallow-copy">shallow copy</a>).
<p> <p>See also <a href="#clear">clear</a>().
<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
</h3>
<p> Returns TRUE if this vector and <em>v</em> are equal; otherwise returns
FALSE.
<h3 class=fn>type * <a name="operator[]"></a>QPtrVector::operator[] ( int&nbsp;i ) const
</h3>
<p> Returns the item at position <em>i</em>, or 0 if there is no item at
that position. <em>i</em> must be less than <a href="#size">size</a>().
<p> Equivalent to <a href="#at">at</a>( <em>i</em> ).
<p> <p>See also <a href="#at">at</a>().
<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>
</h3>
<p> Reads a vector item, <em>item</em>, from the stream <em>s</em> and returns a
reference to the stream.
<p> The default implementation sets <em>item</em> to 0.
<p> <p>See also <a href="#write">write</a>().
<h3 class=fn>bool <a name="remove"></a>QPtrVector::remove ( uint&nbsp;i )
</h3>
<p> Removes the item at position <em>i</em> in the vector, if there is one.
<em>i</em> must be less than <a href="#size">size</a>().
<p> Returns TRUE if <em>i</em> is within range; otherwise returns FALSE.
<p> <p>See also <a href="#take">take</a>() and <a href="#at">at</a>().
<h3 class=fn>bool <a name="resize"></a>QPtrVector::resize ( uint&nbsp;size )
</h3>
<p> Resizes (expands or shrinks) the vector to <em>size</em> elements. The
vector becomes a null vector if <em>size</em> == 0.
<p> Any items at position <em>size</em> or beyond in the vector are removed.
New positions are initialized to 0.
<p> Returns TRUE if successful, i.e. if the memory was successfully
allocated; otherwise returns FALSE.
<p> <p>See also <a href="#size">size</a>() and <a href="#isNull">isNull</a>().
<h3 class=fn>void <a name="setAutoDelete"></a>QPtrCollection::setAutoDelete ( bool&nbsp;enable )
</h3>
<p> Sets the collection to auto-delete its contents if <em>enable</em> is
TRUE and to never delete them if <em>enable</em> is FALSE.
<p> 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.
<p> 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.
<p> Note that the auto-delete setting may also affect other functions
in subclasses. For example, a subclass that has a <a href="#remove">remove</a>()
function will remove the item from its data structure, and if
auto-delete is enabled, will also delete the item.
<p> <p>See also <a href="qptrcollection.html#autoDelete">autoDelete</a>().
<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>.
<h3 class=fn>uint <a name="size"></a>QPtrVector::size () const
</h3>
<p> 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.
<p> The vector is a null vector if <a href="#size">size</a>() == 0.
<p> <p>See also <a href="#isNull">isNull</a>(), <a href="#resize">resize</a>(), and <a href="#count">count</a>().
<h3 class=fn>void <a name="sort"></a>QPtrVector::sort ()
</h3>
<p> Sorts the items in ascending order. Any empty positions will be
put last.
<p> Compares items using the virtual function <a href="#compareItems">compareItems</a>().
<p> <p>See also <a href="#bsearch">bsearch</a>().
<h3 class=fn>type * <a name="take"></a>QPtrVector::take ( uint&nbsp;i )
</h3>
<p> Returns the item at position <em>i</em> in the vector, and removes that
item from the vector. <em>i</em> must be less than <a href="#size">size</a>(). If there is
no item at position <em>i</em>, 0 is returned.
<p> Unlike <a href="#remove">remove</a>(), this function does <em>not</em> call <a href="qptrcollection.html#deleteItem">deleteItem</a>() for
the removed item.
<p> <p>See also <a href="#remove">remove</a>() and <a href="#at">at</a>().
<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>
</h3>
<p> Writes a vector item, <em>item</em>, to the stream <em>s</em> and returns a
reference to the stream.
<p> The default implementation does nothing.
<p> <p>See also <a href="#read">read</a>().
<!-- eof -->
<hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>.
Copyright &copy; 1995-2007
<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>