📄 qdict.html
字号:
<!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/qdict.doc:41 --><html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"><title>QDict Class</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>QDict Class Reference</h1><p>The QDict class is a template class that provides adictionary based on QString keys.<a href="#details">More...</a><p><tt>#include <<a href="qdict-h.html">qdict.h</a>></tt><p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.<p><a href="qdict-members.html">List of all member functions.</a><h2>Public Members</h2><ul><li><div class=fn><a href="#QDict"><b>QDict</b></a> ( int size = 17, bool caseSensitive = TRUE )</div></li><li><div class=fn><a href="#QDict-2"><b>QDict</b></a> ( const QDict<type> & dict )</div></li><li><div class=fn><a href="#~QDict"><b>~QDict</b></a> ()</div></li><li><div class=fn>QDict<type> & <a href="#operator-eq"><b>operator=</b></a> ( const QDict<type> & dict )</div></li><li><div class=fn>virtual uint <a href="#count"><b>count</b></a> () const</div></li><li><div class=fn>uint <a href="#size"><b>size</b></a> () const</div></li><li><div class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</div></li><li><div class=fn>void <a href="#insert"><b>insert</b></a> ( const QString & key, const type * item )</div></li><li><div class=fn>void <a href="#replace"><b>replace</b></a> ( const QString & key, const type * item )</div></li><li><div class=fn>bool <a href="#remove"><b>remove</b></a> ( const QString & key )</div></li><li><div class=fn>type * <a href="#take"><b>take</b></a> ( const QString & key )</div></li><li><div class=fn>type * <a href="#find"><b>find</b></a> ( const QString & key ) const</div></li><li><div class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( const QString & key ) const</div></li><li><div class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</div></li><li><div class=fn>void <a href="#resize"><b>resize</b></a> ( uint newsize )</div></li><li><div class=fn>void <a href="#statistics"><b>statistics</b></a> () const</div></li></ul><h2>Important Inherited Members</h2><ul><li><div class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</div></li><li><div class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool enable )</div></li></ul><h2>Protected Members</h2><ul><li><div class=fn>virtual QDataStream & <a href="#read"><b>read</b></a> ( QDataStream & s, QPtrCollection::Item & item )</div></li><li><div class=fn>virtual QDataStream & <a href="#write"><b>write</b></a> ( QDataStream & s, QPtrCollection::Item ) const</div></li></ul><hr><a name="details"></a><h2>Detailed Description</h2><p> The QDict class is a template class that provides adictionary based on <a href="qstring.html">QString</a> keys.<p> <p> <p> <a href="qmap.html">QMap</a> is an STL-compatible alternative to this class.<p> QDict is implemented as a template class. Define a template instanceQDict<X> to create a dictionary that operates on pointers to X (X*).<p> A dictionary is a collection of key-value pairs. The key is a QStringused for insertion, removal and lookup. The value is a pointer.Dictionaries provide very fast insertion and lookup.<p> If you want to use non-Unicode, plain 8-bit <tt>char*</tt> keys, use the<a href="qasciidict.html">QAsciiDict</a> template. A QDict has the same performance as a QAsciiDict.If you want to have a dictionary that maps QStrings to QStrings useQMap.<p> The <a href="#size">size</a>() of the dictionary is very important. In order to get goodperformance, you should use a suitably large <a href="primes.html">prime number</a>. Suitable means equal to or larger than the maximumexpected number of dictionary items. Size is set in the constructorbut may be changed with <a href="#resize">resize</a>().<p> Items are inserted with <a href="#insert">insert</a>(), and removed with <a href="#remove">remove</a>(). All theitems in a dictionary can be removed with <a href="#clear">clear</a>(). The number of itemsin the dictionary is returned by <a href="#count">count</a>(). If the dictionarycontains no items <a href="#isEmpty">isEmpty</a>() returns TRUE. You can change anitem's value with <a href="#replace">replace</a>(). Items are looked up with <a href="#operator[]">operator[]</a>(),or with <a href="#find">find</a>() which return a pointer to the value or 0 if the givenkey does not exist. You can take an item out of the dictionary with<a href="#take">take</a>().<p> Calling <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE) for a dictionary tells it to delete itemsthat are removed. The default behaviour is not to delete items whenthey are removed.<p> QDict is implemented by QGDict as a hash array with a fixed number ofentries. Each array entry points to a singly linked list of buckets,in which the dictionary items are stored. When an item is insertedwith a key, the key is converted (hashed) to an integer index into thehash array. The item is inserted before the first bucket in the listof buckets.<p> Looking up an item is normally very fast. The key is again hashed to anarray index. Then QDict scans the list of buckets and returns the itemfound or null if the item was not found. You cannot insert null pointersinto a dictionary.<p> Items with equal keys are allowed. When inserting two items with thesame key, only the last inserted item will be visible (last in, first out)until it is removed.<p> The <a href="qdictiterator.html">QDictIterator</a> class can traverse the dictionary, but onlyin an arbitrary order. Multiple iterators may independently traverse thesame dictionary.<p> When inserting an item into a dictionary, only the pointer is copied,not the item itself, i.e. a <a href="shclass.html#shallow-copy">shallow copy</a> is made. It is possible tomake the dictionary copy all of the item's data (a <a href="shclass.html#deep-copy">deep copy</a>)when an item is inserted. <a href="#insert">insert</a>() calls the virtual function<a href="qptrcollection.html#newItem">QPtrCollection::newItem</a>() for the item to be inserted. Inherit adictionary and reimplement it if you want deep copies.<p> When removing a dictionary item, the virtual function<a href="qptrcollection.html#deleteItem">QPtrCollection::deleteItem</a>() is called. QDict's default implementationis to delete the item if auto-deletion is enabled.<p> Example #1:<pre> QDict<QLineEdit> fields; fields.<a href="#insert">insert</a>( "forename", new <a href="qlineedit.html">QLineEdit</a>( this ) ); fields.<a href="#insert">insert</a>( "surname", new <a href="qlineedit.html">QLineEdit</a>( this ) ); fields["forename"]->setText( "Homer" ); fields["surname"]->setText( "Simpson" ); <a href="qdictiterator.html">QDictIterator</a><char> it( extra ); // See QDictIterator for( ; it.<a href="qdictiterator.html#current">current</a>(); ++it ) cout << it.<a href="qdictiterator.html#currentKey">currentKey</a>() << ": " << it.<a href="qdictiterator.html#current">current</a>()->text() << endl; cout << endl; if ( fields["forename"] && fields["surname"] ) cout << fields["forename"]->text() << " " << fields["surname"]->text() << endl; // Prints "Homer Simpson" fields.<a href="#remove">remove</a>( "forename" ); // Does not delete the line edit if ( ! fields["forename"] ) cout << "forename is not in the dictionary" << endl; </pre> In this example we use a dictionary to keep track of the lineedits we're using. We insert each line edit into the dictionarywith a unique name and then access the line edits via thedictionary. <p> Example #2:<p> <pre> <a href="qstringlist.html">QStringList</a> styleList = QStyleFactory::styles(); styleList.<a href="qstringlist.html#sort">sort</a>(); QDict<int> letterDict( 17, FALSE ); for ( QStringList::Iterator it = styleList.<a href="qvaluelist.html#begin">begin</a>(); it != styleList.<a href="qvaluelist.html#end">end</a>(); ++it ) { <a href="qstring.html">QString</a> styleName = *it; <a href="qstring.html">QString</a> styleAccel = styleName; if ( letterDict[styleAccel.<a href="qstring.html#left">left</a>(1)] ) { for ( uint i = 0; i < styleAccel.<a href="qstring.html#length">length</a>(); i++ ) { if ( ! letterDict[styleAccel.<a href="qstring.html#mid">mid</a>( i, 1 )] ) { styleAccel = styleAccel.<a href="qstring.html#insert">insert</a>( i, '&' ); letterDict.<a href="#insert">insert</a>(styleAccel.<a href="qstring.html#mid">mid</a>( i, 1 ), (const int *)1); break; } } } else { styleAccel = "&" + styleAccel; letterDict.<a href="#insert">insert</a>(styleAccel.<a href="qstring.html#left">left</a>(1), (const int *)1); } (void) new <a href="qaction.html">QAction</a>( styleName, QIconSet(), styleAccel, parent ); } </pre> In the example we are using the dictionary to provide fast randomaccess to the keys, and we don't care what the values are. The exampleis used to generate a menu of QStyles, each with a uniqueaccelerator key (or no accelerator if there are no unused lettersleft).<p> We first obtain the list of available styles, then sort them so thatthe menu items will be ordered alphabetically. Next we create adictionary of int pointers. The keys in the dictionary are each onecharacter long, representing letters that have been used foraccelerators. We iterate through our list of style names. If thefirst letter of the style name is in the dictionary, i.e. has beenused, we iterate over all the characters in the style name to see ifwe can find a letter that hasn't been used. If we find an unusedletter we put the accelerator ampersand (&) in front of it and addthat letter to the dictionary. If we can't find an unused letter thestyle will simply have no accelerator. If the first letter of thestyle name is not in the dictionary we use it for the acceleratorand add it to the dictionary. Finally we create a <a href="qaction.html">QAction</a> for eachstyle.<p> <p>See also <a href="qdictiterator.html">QDictIterator</a>, <a href="qasciidict.html">QAsciiDict</a>, <a href="qintdict.html">QIntDict</a>, <a href="qptrdict.html">QPtrDict</a>, <a href="collection.html">Collection Classes</a> and <a href="tools.html">Non-GUI Classes</a>.<hr><h2>Member Function Documentation</h2><h3 class=fn><a name="QDict"></a>QDict::QDict ( int size = 17, bool caseSensitive = TRUE )</h3><p> Constructs a dictionary optimized for less than <em>size</em> entries.<p> We recommend setting <em>size</em> to a suitably large prime number (e.g. a
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -