📄 qtl.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Qt Toolkit - 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: white; color: black; }--></style></head><body bgcolor="#ffffff"><p><table width="100%"><tr><td><a href="index.html"><img width="100" height="100" src="qtlogo.png"alt="Home" border="0"><img width="100"height="100" src="face.png" alt="Home" border="0"></a><td valign="top"><div align="right"><img src="dochead.png" width="472" height="27"><br><a href="classes.html"><b>Classes</b></a>- <a href="annotated.html">Annotated</a>- <a href="hierarchy.html">Tree</a>- <a href="functions.html">Functions</a>- <a href="index.html">Home</a>- <a href="topicals.html"><b>Structure</b> <font face="Arial,Helvetica,Geneva,Swiss,SunSans-Regular" align="center" size=32>Qte</font></a></div></table><h1 align="center"> Qt Template library</h1><br clear="all">Thq Qt Template Library is a set of templates within Qt dealing withcontainers of objects. It provides a list of objects, a stack ofobjects, a map (or dictionary) from one type to another, andassociated iterators and algorithms.<p>Qt also contains similar classes that deal with pointers to objects;<a href="qvaluelist.html">QValueList</a> vs. <a href="qlist.html">QList</a>, etc. Compared to the pointer-basedtemplates, the QTL offers easy copying of the container, real supportfor classes that e.g. require constructors, expand to much more objectcode, can often be a bit faster, require that the objects stored canbe copied, and finally, have a worse record of compiler problems.<p>Compared to the STL, the QTL contains only the most important featuresof the STL, has more regular function naming, has no platformdifferences, is often a little slower and often expands to less objectcode.<p>If you can not make copies of the objects you want to store you arebetter off with QCollection and friends. They were designed to handleexactly that kind of pointer semantics. This applies for example toall classes derived from <a href="qobject.html">QObject</a>. A QObject does not have a copyconstructor, so using it as value is impossible. You may choose bestore pointers to QObjects in a QValueList, but using QList directlyseems to be the better choice for this kind of applicationdomain. QList, like all other QCollection based containers, providesfar more sanity checking than a speed-optimized valuebased container.<p>If you have objects that implement value semantics, use the Qttemplate library. Value semantics require at least<ul><li>a copy constructor,<li>an assignment operator and<li> a default constructor, i.e. a constructor that does not takeany arguments.</ul>Note that a fast copy constructor is absolutely crucial for a goodoverall performance of the container, since many copy operations aregoing to happen.<p>Examples for value based classes are QRect, QPoint, QSize and allsimple C++ types like int, bool or double.<p>The Qt template library is designed for speed. Especially iteratorsare extremely fast. On the drawback side, less error checking is donethan in the QCollection based containers. A template library containerfor example does not track associated iterators. This makes certainvalidity checks, like on removing items, impossible to performautomatically.<p><h2> Iterators </h2><p>The Qt template library deals with value objects, not with pointers.For that reason, there is no other way of iterating over containersthan using iterators. This is no disadvantage as the size of aniterator matches the size of a normal pointer - 32 or 64 bitsdepending on your CPU architecture.<p>To iterate over a container, use a loop like this:<p><pre> typedef QValueList<int> 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, whileend() returns an iterator that points <em>after</em> the lastelement. end() marks an invalid position, it can never bedereferenced. It's the break condition in any iteration, may it befrom begin() or fromLast(). For maximum speed, use increment ordecrement iterators with the prefix operator (++it, --it) instead of the thepostfix one (it++, it--), since the former is slightly faster.<p>The same concept applies to the other container classes:<p><pre> typedef QMap<<a href="qstring.html">QString</a>,QString> 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 QArray<int> Array; Array array; for( Array::Iterator it = array.begin(); it != array.end(); ++it ) printf("Data=%i\n", *it );</pre><p>There are two kind of iterators, the volatile iterator shown in theexamples above and a version that returns a const reference to itscurrent object, the ConstIterator. Const iterators are requiredwhenever the container itself is const, such as a member variableinside a const function. Assigning a ConstIterator to a normalIterator is not allowed as it would violate const semantics.<p><h2> Algorithms </h2><p>The template library defines a number of algorithms that operate onits containers: qHeapSort(), qBubbleSort(), qSwap() andqCopy(). These algorithms are implemented as template functions.<p>qHeapSort() and qBubbleSort() provide the well known sortingalgorithms. You can use them like this:<p><pre> typedef QValueList<int> List; List l; l << 42 << 100 << 1234 << 12 << 8; qHeapSort( l ); List l2; l2 << 42 << 100 << 1234 << 12 << 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 allelements enclosed in the two iterators, namely 100, 1234 and 12. Thethird example shows that iterators act like pointers and can betreated as such.<p>Naturally, the sorting templates won't work with const iterators.<p>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>Another template function is qCopy(). It copies a container or a sliceof it to an OutputIterator, in this case a QTextOStreamIterator:<p><pre> typedef QValueList<int> List; List l; l << 100 << 200 << 300; <a href="qtextostream.html">QTextOStream</a> str( stdout ); qCopy( l, QTextOStreamIterator( str ) );</pre><p>In addition, you can use any Qt template library iterator as theOutputIterator. Just make sure that the right hand of the iterator hasas many elements present as you want to insert. The following exampleillustrates this:<p><pre> <a href="qstringlist.html">QStringList</a> l1, l2; l1 << "Weis" << "Ettrich" << "Arnt" << "Sue"; l2 << "Torben" << "Matthias"; qCopy( l2, l1.begin() );</pre><p>At the end of this code fragment, the List l1 contains "Torben","Matthias", "Arnt" and "Sue", with the prior contents beingoverwritten. Another flavor of qCopy() takes three arguments to makeit possible to copy a slice of a container:<p><pre> typedef QValueList<int> List; List l; l << 42 << 100 << 1234 << 12 << 8; List::Iterator b = l.find( 100 ); List::Iterator e = l.find( 8 ); <a href="qtextostream.html">QTextOStream</a> str( stdout ); qCopy( b, e, QTextOStreamIterator( str ) );</pre><p>If you write new algorithms, consider writing them as templatefunctions in order to make them usable with as many containerspossible. In the above example, you could just as easily print out astandard 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><p>All mentioned containers can be serialized with the respectivestreaming operators. Here is an example.<p><pre> <a href="qdatastream.html">QDataStream</a> str(...); <a href="qvaluelist.html">QValueList</a><<a href="qrect.html">QRect</a>> l; // ... fill the list here str << l;</pre><p>The container can be read in again with:<p><pre> <a href="qvaluelist.html">QValueList</a><<a href="qrect.html">QRect</a>> l; str >> l;</pre><p>The same applies to QStringList, QValueStack and QMap.<p>Classes:<ul plain><li><a href="qmap.html">QMap</a> (Value based template class that provides a dictionary)<li><a href="qmapconstiterator.html">QMapConstIterator</a> (Iterator for <a href="qmap.html">QMap</a>)<li><a href="qmapiterator.html">QMapIterator</a> (Iterator for <a href="qmap.html">QMap</a>)<li><a href="qstringlist.html">QStringList</a> (A list of strings)<li><a href="qvaluelist.html">QValueList</a> (Value based template class that provides doubly linked lists)<li><a href="qvaluelistconstiterator.html">QValueListConstIterator</a> (Iterator for <a href="qvaluelist.html">QValueList</a>)<li><a href="qvaluelistiterator.html">QValueListIterator</a> (Iterator for <a href="qvaluelist.html">QValueList</a>)<li><a href="qvaluestack.html">QValueStack</a> (Value based template class that provides a stack)</ul><p><address><hr><div align="center"><table width="100%" cellspacing="0" border="0"><tr><td>Copyright
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -