Sophie

Sophie

distrib > Mandriva > 8.2 > i586 > media > contrib > by-pkgid > 112b0974ad288f6cd55bf971ee6026a9 > files > 1885

libqt3-devel-3.0.2-2mdk.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /tmp/qt-3.0-reggie-28534/qt-x11-free-3.0.2/doc/qtl.doc:36 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Qt Template Library</title>
<style type="text/css"><!--
h3.fn,span.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>Qt Template Library</h1>

 
<p> 
<p> The Qt Template Library (QTL) is a set of templates that provide
object containers.  If a suitable STL implementation is not
available for your compiler, the QTL can be used instead.  It
provides a list of objects, a vector (dynamic array) of objects, a map
(or dictionary) from one type to another, and associated <a
href="#Iterators">iterators</a> and <a
href="#Algorithms">algorithms</a>.  A container is an object which
contains and manages other objects and provides iterators that allow
the contained objects to be accessed.
<p> The QTL classes' naming conventions are consistent with the other Qt
classes (e.g., count(), isEmpty()). They also provide extra functions
for compatibility with STL algorithms, such as size() and empty().
Programmers already familiar with the STL <tt>map</tt> can use these
functions instead.
<p> Compared to the STL, the QTL contains only the most important
features of the STL container API, has no platform differences, is
often a little slower and often expands to less object code.
<p> If you cannot make copies of the objects you want to store you are
better off with <a href="qptrcollection.html">QPtrCollection</a> and friends. They were designed to handle
exactly that kind of pointer semantics. This applies for example to
all classes derived from <a href="qobject.html">QObject</a>. A <a href="qobject.html">QObject</a> does not have a copy
constructor, so using it as value is impossible. You may choose to
store pointers to QObjects in a <a href="qvaluelist.html">QValueList</a>, but using <a href="qptrlist.html">QPtrList</a> directly
seems to be the better choice for this kind of application
domain. QPtrList, like all other QPtrCollection based containers, provides
far more sanity checking than a speed-optimized value based container.
<p> If you have objects that implement value semantics, and the STL is not
available on your target platform, the Qt Template Library can be used
instead.  Value semantics require at least 
<p> <ul>
<li> a copy constructor, 
<li> an assignment operator and 
<li> a defaultconstructor, i.e. a constructor that does not take any arguments.
</ul>
<p> Note that a fast copy constructor is absolutely crucial for a good
overall performance of the container, since many copy operations are
going to happen.
<p> If you intend sorting your data you must implement <a href="qcstring.html#operator-lt-2">operator&lt;</a>() for
your data's class.
<p> Good candidates for value based classes are <a href="qrect.html">QRect</a>, <a href="qpoint.html">QPoint</a>, <a href="qsize.html">QSize</a>,
<a href="qstring.html">QString</a> and all simple C++ types, such as int, bool or double.
<p> The Qt Template Library is designed for speed. Iterators are extremely
fast. To achieve this performance, less error checking is done than in
the <a href="qptrcollection.html">QPtrCollection</a> based containers. A QTL container, for example,
does not track any associated iterators. This makes certain validity
checks, for example when removing items, impossible to perform
automatically, however it provides extremely good performance.
<p> <a name="Iterators"></a>
<h2> Iterators
</h2>
<a name="1"></a><p> The Qt Template Library deals with value objects, not with pointers.
For that reason, there is no other way of iterating over containers
other than with iterators. This is no disadvantage as the size of an
iterator matches the size of a normal pointer.
<p> To iterate over a container, use a loop like this:
<p> <pre>
    typedef QValueList&lt;int&gt; List;
    List l;
    for( List::Iterator it = l.begin(); it != l.end(); ++it )
        printf( "Number is %i\n", *it );
</pre>
 
<p> begin() returns the iterator pointing at the first element, while
end() returns an iterator that points <em>after</em> the last
element. end() marks an invalid position, it can never be
dereferenced. It's the break condition in any iteration, may it be
from begin() or fromLast(). For maximum speed, use increment or
decrement iterators with the prefix operator (++it, --it) instead of the
postfix one (it++, it--), since the former is slightly faster.
<p> The same concept applies to the other container classes:
<p> <pre>
    typedef QMap&lt;QString,QString&gt; Map;
    Map map;
    for( Map::iterator it = map.begin(); it != map.end(); ++it )
        printf( "Key=%s Data=%s\n", it.key().ascii(), it.data().ascii() );

    typedef QValueVector&lt;int&gt; Vector;
    Vector vec;
    for( Vector::iterator it = vec.begin(); it != vec.end(); ++it )
        printf( "Data=%d\n", *it );
</pre>
 
<p> There are two kind of iterators, the volatile iterator shown in the
examples above and a version that returns a const reference to its
current object, the ConstIterator. Const iterators are required
whenever the container itself is const, such as a member variable
inside a const function. Assigning a ConstIterator to a normal
Iterator is not allowed as it would violate const semantics.
<p> <a name="Algorithms"></a>
<h2> Algorithms
</h2>
<a name="2"></a><p> The Qt Template Library defines a number of algorithms that operate on
its containers.  These algorithms are implemented as template
functions and provide useful generic code which can be applied to any
container that provides iterators (even your own containers).
<p> qHeapSort() and qBubbleSort() provide the well known sorting
algorithms. You can use them like this:
<p> <pre>
    typedef QValueList&lt;int&gt; List;
    List l;
    l &lt;&lt; 42 &lt;&lt; 100 &lt;&lt; 1234 &lt;&lt; 12 &lt;&lt; 8;
    qHeapSort( l );

    List l2;
    l2 &lt;&lt; 42 &lt;&lt; 100 &lt;&lt; 1234 &lt;&lt; 12 &lt;&lt; 8;
    List::Iterator b = l2.find( 100 );
    List::Iterator e = l2.find( 8 );
    qHeapSort( b, e );

    double arr[] = { 3.2, 5.6, 8.9 };
    qHeapSort( arr, arr + 3 );
</pre>
 
<p> The first example sorts the entire list. The second one sorts all
elements enclosed in the two iterators, namely 100, 1234 and 12.  The
third example shows that iterators act like pointers and can be
treated as such.
<p> If using your own data types you must implement <a href="qcstring.html#operator-lt-2">operator&lt;</a>() for
your data's class.
<p> Naturally, the sorting templates won't work with const iterators.
<p> <a name="qSwap"></a>
Another utility is qSwap(). It exchanges the values of two variables:
<p> <pre>
    <a href="qstring.html">QString</a> second( "Einstein" );
    <a href="qstring.html">QString</a> name( "Albert" );
    qSwap( second, name );
</pre>
 
<p> <a name="qCount"></a>
Another template function is qCount().  It counts the number of
occurrences of a value within a container.  For example:
<pre>
    <a href="qvaluelist.html">QValueList</a>&lt;int&gt; l;
    l.<a href="qvaluelist.html#push_back">push_back</a>( 1 );          
    l.<a href="qvaluelist.html#push_back">push_back</a>( 1 );          
    l.<a href="qvaluelist.html#push_back">push_back</a>( 1 );          
    l.<a href="qvaluelist.html#push_back">push_back</a>( 2 );          
    int c = 0;
    qCount( l.<a href="qvaluelist.html#begin">begin</a>(), l.<a href="qvaluelist.html#end">end</a>(), 1, c ); // c == 3
</pre>
 	
<p> <a name="qFind"></a>
Another template function is qFind.  It find the first occurrence of a
value within a container. For example:
<p> <pre>
    <a href="qvaluelist.html">QValueList</a>&lt;int&gt; l;
    l.<a href="qvaluelist.html#push_back">push_back</a>( 1 );          
    l.<a href="qvaluelist.html#push_back">push_back</a>( 1 );          
    l.<a href="qvaluelist.html#push_back">push_back</a>( 1 );          
    l.<a href="qvaluelist.html#push_back">push_back</a>( 2 );          
    <a href="qvaluelistiterator.html">QValueListIterator</a>&lt;int&gt; it = qFind( l.<a href="qvaluelist.html#begin">begin</a>(), l.<a href="qvaluelist.html#end">end</a>(), 2 );
</pre>
 	
<p> <a name="qFill"></a>
Another template function is qFill.  It fills a
range with copies of a value.  For example:
<p> <pre>
    <a href="qvaluevector.html">QValueVector</a>&lt;int&gt; v(3);
    qFill( v.<a href="qvaluevector.html#begin">begin</a>(), v.<a href="qvaluevector.html#end">end</a>(), 99 ); // v contains 99, 99, 99
</pre>
 
<p> <a name="qEqual"></a>
Another template function is qEqual.  It compares
two ranges for equality of their elements.  Note that the number of
elements in each range is not considered, only if the elements in the
first range are equal to the corresponding elements in the second
range (consequently, both ranges must be valid).  For example:
<p> <pre>
    <a href="qvaluevector.html">QValueVector</a>&lt;int&gt; v1(3);
    v1[0] = 1;
    v1[2] = 2;
    v1[3] = 3;

    <a href="qvaluevector.html">QValueVector</a>&lt;int&gt; v2(5);
    v1[0] = 1;
    v1[2] = 2;
    v1[3] = 3;
    v1[4] = 4;
    v1[5] = 5;

    bool b = qEqual( v1.<a href="qvaluevector.html#begin">begin</a>(), v2.<a href="qvaluevector.html#end">end</a>(), v2.<a href="qvaluevector.html#begin">begin</a>() );
    // b == TRUE
</pre>
 
<p> <a name="qCopy"></a>
Another template function is qCopy(). It copies a
range of elements to an OutputIterator, in this case a
QTextOStreamIterator:
<p> <pre>
    <a href="qvaluelist.html">QValueList</a>&lt;int&gt; l;
    l.<a href="qvaluelist.html#push_back">push_back</a>( 100 );
    l.<a href="qvaluelist.html#push_back">push_back</a>( 200 );
    l.<a href="qvaluelist.html#push_back">push_back</a>( 300 );
    <a href="qtextostream.html">QTextOStream</a> str( stdout );
    qCopy( l.<a href="qvaluelist.html#begin">begin</a>(), l.<a href="qvaluelist.html#end">end</a>(), QTextOStreamIterator(str) );
</pre>
 
<p> 
<p> <a name="qCopyBackward"></a>
Another template function is
qCopyBackward().  It copies a container or a slice of it to an
OutputIterator, but in backwards fashion, for example:
<p> <pre>
    <a href="qvaluevector.html">QValueVector</a>&lt;int&gt; vec(3);
    vec.<a href="qvaluevector.html#push_back">push_back</a>( 100 );
    vec.<a href="qvaluevector.html#push_back">push_back</a>( 200 );
    vec.<a href="qvaluevector.html#push_back">push_back</a>( 300 );
    <a href="qvaluevector.html">QValueVector</a>&lt;int&gt; another;
    qCopyBackward( vec.<a href="qvaluevector.html#begin">begin</a>(), vec.<a href="qvaluevector.html#end">end</a>(), another.<a href="qvaluevector.html#begin">begin</a>() );
    // 'another' now contains 100, 200, 300
    // however the elements are copied one at a time 
    // in reverse order (300, 200, then 100)
</pre>
 
<p> <a name="qMakePair"></a>
Another template function is qMakePair(). This is a convenience
function which is used for creating <a href="qpair.html">QPair</a>&lt;&gt; objects.  For example:
<pre>
    <a href="qmap.html">QMap</a>&lt;QString,QString&gt; m;
    m.<a href="qmap.html#insert">insert</a>( qMakePair("Clinton", "Bill") );
</pre>
 
<p> The above code is equivalent to:
<pre>
    <a href="qmap.html">QMap</a>&lt;QString,QString&gt; m;
    <a href="qpair.html">QPair</a>&lt;QString,QString&gt; p( "Clinton", "Bill" );
    m.<a href="qmap.html#insert">insert</a>( p );
</pre>
 
<p> In addition, you can use any Qt Template Library iterator as the
OutputIterator. Just make sure that the right hand of the iterator has
as many elements present as you want to insert. The following example
illustrates this:
<p> <pre>
    <a href="qstringlist.html">QStringList</a> l1, l2;
    l1 &lt;&lt; "Weis" &lt;&lt; "Ettrich" &lt;&lt; "Arnt" &lt;&lt; "Sue";
    l2 &lt;&lt; "Torben" &lt;&lt; "Matthias";
    qCopy( l2.begin(), l2.end(), l1.<a href="qvaluelist.html#begin">begin</a>() );

    <a href="qvaluevector.html">QValueVector</a>&lt;QString&gt; v( l1.<a href="qvaluelist.html#size">size</a>(), "Dave" );
    qCopy( l2.begin(), l2.end(), v.<a href="qvaluevector.html#begin">begin</a>() );
</pre>
 
<p> At the end of this code fragment, the list l1 contains "Torben",
"Matthias", "Arnt" and "Sue", with the prior contents being
overwritten.  The vector v contains "Torben", "Matthias", "Dave"
and "Dave, also with the prior contents being overwritten.
<p> If you write new algorithms, consider writing them as template
functions in order to make them usable with as many containers
possible.  In the above example, you could just as easily print out a
standard C++ array with qCopy():
<p> <pre>
    int arr[] = { 100, 200, 300 };
    <a href="qtextostream.html">QTextOStream</a> str( stdout );
    qCopy( arr, arr + 3, QTextOStreamIterator( str ) ); 
</pre>
 
<p> <h2> Streaming
</h2>
<a name="3"></a><p> All mentioned containers can be serialized with the respective
streaming operators. Here is an example.
<p> <pre>
    <a href="qdatastream.html">QDataStream</a> str(...);
    <a href="qvaluelist.html">QValueList</a>&lt;QRect&gt; l;
    // ... fill the list here
    str &lt;&lt; l;
</pre>
 
<p> The container can be read in again with:
<p> <pre>
    <a href="qvaluelist.html">QValueList</a>&lt;QRect&gt; l;
    str &gt;&gt; l;
</pre>
 
<p> The same applies to <a href="qstringlist.html">QStringList</a>, <a href="qvaluestack.html">QValueStack</a> and <a href="qmap.html">QMap</a>.

<!-- eof -->
<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2001 
<a href="http://www.trolltech.com">Trolltech</a><td><a href="http://www.trolltech.com/trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt version 3.0.2</div>
</table></div></address></body>
</html>