collection.html
来自「QT 下载资料仅供参考」· HTML 代码 · 共 247 行
HTML
247 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"><!-- /home/reggie/tmp/qt-3.0-reggie-5401/qt-x11-commercial-3.0.5/doc/collect.doc:36 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>Collection Classes</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 Classes</font></a> | <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> | <a href="annotated.html"><font color="#004faf">Annotated</font></a> | <a href="groups.html"><font color="#004faf">Grouped 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>Collection Classes</h1> <p> <!-- index collection classes --><a name="collection-classes"></a><p> A collection class is a container which holds a number of items in acertain data structure and performs operations on the contained items;insert, remove, find etc.<p> Qt has several value-based and several pointer-based collectionclasses. The pointer-based collection classes work with pointers toitems, while the value-based classes store copies of their items. Thevalue-based collections are very similar to STL container classes, andcan be used with STL algorithms and containers. See the <a href="qtl.html">Qt Template Library</a> documentation for details.<p> The value-based collections are:<ul><li> <a href="qvaluelist.html">QValueList</a>, a value-based list<li> <a href="qvaluevector.html">QValueVector</a>, a value-based vector structure<li> <a href="qvaluestack.html">QValueStack</a>, a value-based stack structure<li> <a href="qmap.html">QMap</a>, a value-based dictionary structure</ul><p> The pointer-based collections are:<ul><li> <a href="qcache.html">QCache</a> and <a href="qintcache.html">QIntCache</a>,LRU (least recently used) cache structures.<li> <a href="qdict.html">QDict</a>, <a href="qintdict.html">QIntDict</a> and <a href="qptrdict.html">QPtrDict</a> dictionary structures.<li> <a href="qptrlist.html">QPtrList</a>, a double linked list structure.<li> <a href="qptrqueue.html">QPtrQueue</a>, a FIFO (first in, first out) queue structure.<li> <a href="qptrstack.html">QPtrStack</a>, a LIFO (last in, first out) stack structure.<li> <a href="qptrvector.html">QPtrVector</a>, a vector structure.</ul><p> <a href="qmemarray.html">QMemArray</a> is exceptional; it is neither pointer nor value based,but memory based. For maximum efficiency with the simple data typesusually used in arrays, it uses bitwise operations to copy and comparearray elements.<p> Some of these classes have corresponding iterators. An iteratoris a class for traversing the items in a collection:<ul><li> <a href="qcacheiterator.html">QCacheIterator</a> and<a href="qintcacheiterator.html">QIntCacheIterator</a><li> <a href="qdictiterator.html">QDictIterator</a>,<a href="qintdictiterator.html">QIntDictIterator</a>, and<a href="qptrdictiterator.html">QPtrDictIterator</a><li> <a href="qptrlistiterator.html">QPtrListIterator</a><li> <a href="qvaluelistiterator.html">QValueListIterator</a>, and<a href="qvaluelistconstiterator.html">QValueListConstIterator</a><li> <a href="qmapiterator.html">QMapIterator</a>, and<a href="qmapconstiterator.html">QMapConstIterator</a></ul><p> The value-based collections plus algorithms operating on them aregrouped together in the <a href="qtl.html">Qt TemplateLibrary</a>. See the respective documentation for details. <p> The rest of this page dicusses the pointer-based containers.<p> <h2> Architecture of the pointer-based containers</h2><a name="1"></a><p> There are four internal base classes for the pointer-basedcontainers (QGCache, QGDict, QGList and QGVector) that operate onvoid pointers. A thin template layer implements the actualcollections by casting item pointers to and from void pointers.<p> This strategy allows Qt's templates to be very economical on space(instantiating one of these templates adds only inlinable calls tothe base classes), while it does not hurt performance.<p> <h2> A <a href="qptrlist.html">QPtrList</a> Example</h2><a name="2"></a><p> This example shows how to store Employee items in a list and printsthem out in the reverse order:<p> <pre> #include <<a href="qptrlist-h.html">qptrlist.h</a>> #include <<a href="qstring-h.html">qstring.h</a>> #include <stdio.h> class Employee { public: Employee( const char *name, int salary ) { n=name; s=salary; } const char *name() const { return n; } int salary() const { return s; } private: <a href="qstring.html">QString</a> n; int s; }; int main() { <a href="qptrlist.html">QPtrList</a><Employee> list; // list of pointers to Employee list.<a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>( TRUE ); // delete items when they are removed list.<a href="qptrlist.html#append">append</a>( new Employee("Bill", 50000) ); list.<a href="qptrlist.html#append">append</a>( new Employee("Steve",80000) ); list.<a href="qptrlist.html#append">append</a>( new Employee("Ron", 60000) ); <a href="qptrlistiterator.html">QPtrListIterator</a><Employee> it(list); // iterator for employee list for ( it.<a href="qptrlistiterator.html#toLast">toLast</a>(); it.<a href="qptrlistiterator.html#current">current</a>(); --it) ) { Employee *emp = it.<a href="qptrlistiterator.html#current">current</a>(); printf( "%s earns %d\n", emp->name(), emp->salary() ); } return 0; }</pre> <p> Program output:<pre> Ron earns 60000 Steve earns 80000 Bill earns 50000</pre> <p> <h2> Managing Collection Items</h2><a name="3"></a><p> All pointer-based collections inherit the <a href="qptrcollection.html">QPtrCollection</a> base class.This class knows only the number of items in the collection and thedelete strategy.<p> Items in a collection are by default not deleted when they are removedfrom the collection. The <a href="qptrcollection.html#setAutoDelete">QPtrCollection::setAutoDelete</a>() functionspecifies the delete strategy. In the list example, we enableauto-deletion to make the list delete the items when they are removedfrom the list.<p> When inserting an item into a collection, only the pointer is copied,not the item itself. This is called a <a href="shclass.html#shallow-copy">shallow copy</a>. It is possible tomake the collection copy all of the item's data (known as a <a href="shclass.html#deep-copy">deep copy</a>)when an item is inserted. All collection functions that insert anitem call the virtual function <a href="qptrcollection.html#newItem">QPtrCollection::newItem</a>() for the itemto be inserted. Inherit a collection and reimplement it if you wantto have deep copies in your collection.<p> When removing an item from a list, the virtual function <a href="qptrcollection.html#deleteItem">QPtrCollection::deleteItem()</a> is called.The default implementation in all collection classes deletes the itemif auto-deletion is enabled.<p> <h2> Usage</h2><a name="4"></a><p> A pointer-based collection class, such as <a href="qptrlist.html">QPtrList</a><type>, defines acollection of <em>pointers</em> to <em>type</em> objects. The pointer (*) isimplicit.<p> We discuss <a href="qptrlist.html">QPtrList</a> here, but the same techniques apply for allpointer-based collection classes and all collection class iterators.<p> Template instantiation:<pre> <a href="qptrlist.html">QPtrList</a><Employee> list; // wherever the list is used</pre> <p> The item's class or type, Employee in our example, must be defined priorto the list definition.<p> <pre> // Does not work: Employee is not defined class Employee; <a href="qptrlist.html">QPtrList</a><Employee> list; // This works: Employee is defined before it is used class Employee { ... }; <a href="qptrlist.html">QPtrList</a><Employee> list;</pre> <p> <h2> Iterators</h2><a name="5"></a><p> Although <a href="qptrlist.html">QPtrList</a> has member functions to traverse the list, it canoften be better to make use of an iterator. <a href="qptrlistiterator.html">QPtrListIterator</a> is verysafe and can traverse lists that are being modified at the same time.Multiple iterators can work independently on the same collection.<p> A <a href="qptrlist.html">QPtrList</a> has an internal list of all iterators that are currently operatingon the list. When a list entry is removed, the list updates all iteratorsto point to this entry.<p> The <a href="qdict.html">QDict</a> and <a href="qcache.html">QCache</a> collections have no traversal functions. Totraverse these collections, you must use <a href="qdictiterator.html">QDictIterator</a> or <a href="qcacheiterator.html">QCacheIterator</a>.<p> <h2> Predefined Collections</h2><a name="6"></a><p> Qt has the following predefined collection classes:<ul><li> String lists: <a href="qstrlist.html">QStrList</a>, <a href="qstrilist.html">QStrIList</a> (<a href="qstrlist-h.html">qstrlist.h</a>) and<a href="qstringlist.html">QStringList</a> (<a href="qstringlist-h.html">qstringlist.h</a>)<li> String vectors: QStrVec and QStrIVec (qstrvec.h); these are obsolete</ul><p> In almost all cases you would choose <a href="qstringlist.html">QStringList</a>, a valuelist of <a href="shclass.html#implicitly-shared">implicitly shared</a> <a href="qstring.html">QString</a> unicode strings. QPtrStrList andQPtrStrIList store only char pointers, not the strings themselves.<p> <h2> List of Pointer-based Collection Classes and RelatedIterator Classes</h2><a name="7"></a><p> <p><table width="100%"><tr bgcolor=#f0f0f0><td><b><a href="qasciicache.html">QAsciiCache</a></b><td>Template class that provides a cache based on char* keys<tr bgcolor=#f0f0f0><td><b><a href="qasciicacheiterator.html">QAsciiCacheIterator</a></b><td>Iterator for QAsciiCache collections<tr bgcolor=#f0f0f0><td><b><a href="qasciidict.html">QAsciiDict</a></b><td>Template class that provides a dictionary based on char* keys<tr bgcolor=#f0f0f0><td><b><a href="qasciidictiterator.html">QAsciiDictIterator</a></b><td>Iterator for QAsciiDict collections<tr bgcolor=#f0f0f0><td><b><a href="qbitarray.html">QBitArray</a></b><td>Array of bits<tr bgcolor=#f0f0f0><td><b><a href="qbitval.html">QBitVal</a></b><td>Internal class, used with QBitArray<tr bgcolor=#f0f0f0><td><b><a href="qbuffer.html">QBuffer</a></b><td>I/O device that operates on a QByteArray<tr bgcolor=#f0f0f0><td><b><a href="qbytearray.html">QByteArray</a></b><td>Array of bytes<tr bgcolor=#f0f0f0><td><b><a href="qcache.html">QCache</a></b><td>Template class that provides a cache based on QString keys<tr bgcolor=#f0f0f0><td><b><a href="qcacheiterator.html">QCacheIterator</a></b><td>Iterator for QCache collections<tr bgcolor=#f0f0f0><td><b><a href="qcstring.html">QCString</a></b><td>Abstraction of the classic C zero-terminated char array (char *)<tr bgcolor=#f0f0f0><td><b><a href="qdict.html">QDict</a></b><td>Template class that provides a dictionary based on QString keys<tr bgcolor=#f0f0f0><td><b><a href="qdictiterator.html">QDictIterator</a></b><td>Iterator for QDict collections<tr bgcolor=#f0f0f0><td><b><a href="qintcache.html">QIntCache</a></b><td>Template class that provides a cache based on long keys<tr bgcolor=#f0f0f0><td><b><a href="qintcacheiterator.html">QIntCacheIterator</a></b><td>Iterator for QIntCache collections<tr bgcolor=#f0f0f0><td><b><a href="qintdict.html">QIntDict</a></b><td>Template class that provides a dictionary based on long keys<tr bgcolor=#f0f0f0><td><b><a href="qintdictiterator.html">QIntDictIterator</a></b><td>Iterator for QIntDict collections<tr bgcolor=#f0f0f0><td><b><a href="qobjectlist.html">QObjectList</a></b><td>QPtrList of QObjects<tr bgcolor=#f0f0f0><td><b><a href="qobjectlistit.html">QObjectListIt</a></b><td>Iterator for QObjectLists<tr bgcolor=#f0f0f0><td><b><a href="qptrcollection.html">QPtrCollection</a></b><td>The base class of most pointer-based Qt collections<tr bgcolor=#f0f0f0><td><b><a href="qptrdict.html">QPtrDict</a></b><td>Template class that provides a dictionary based on void* keys<tr bgcolor=#f0f0f0><td><b><a href="qptrdictiterator.html">QPtrDictIterator</a></b><td>Iterator for QPtrDict collections<tr bgcolor=#f0f0f0><td><b><a href="qptrlist.html">QPtrList</a></b><td>Template class that provides doubly-linked lists<tr bgcolor=#f0f0f0><td><b><a href="qptrlistiterator.html">QPtrListIterator</a></b><td>Iterator for QPtrList collections<tr bgcolor=#f0f0f0><td><b><a href="qptrqueue.html">QPtrQueue</a></b><td>Template class that provides a queue<tr bgcolor=#f0f0f0><td><b><a href="qstrilist.html">QStrIList</a></b><td>Doubly-linked list of char* with case-insensitive comparison<tr bgcolor=#f0f0f0><td><b><a href="qstrlist.html">QStrList</a></b><td>Doubly-linked list of char*</table><!-- eof --><p><address><hr><div align=center><table width=100% cellspacing=0 border=0><tr><td>Copyright © 2002 <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.5</div></table></div></address></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?