source: trunk/doc/html/qdict.html

Last change on this file was 190, checked in by rudi, 14 years ago

reference documentation added

File size: 21.1 KB
Line 
1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/doc/qdict.doc:41 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QDict Class</title>
7<style type="text/css"><!--
8fn { margin-left: 1cm; text-indent: -1cm; }
9a:link { color: #004faf; text-decoration: none }
10a:visited { color: #672967; text-decoration: none }
11body { background: #ffffff; color: black; }
12--></style>
13</head>
14<body>
15
16<table border="0" cellpadding="0" cellspacing="0" width="100%">
17<tr bgcolor="#E5E5E5">
18<td valign=center>
19 <a href="index.html">
20<font color="#004faf">Home</font></a>
21 | <a href="classes.html">
22<font color="#004faf">All&nbsp;Classes</font></a>
23 | <a href="mainclasses.html">
24<font color="#004faf">Main&nbsp;Classes</font></a>
25 | <a href="annotated.html">
26<font color="#004faf">Annotated</font></a>
27 | <a href="groups.html">
28<font color="#004faf">Grouped&nbsp;Classes</font></a>
29 | <a href="functions.html">
30<font color="#004faf">Functions</font></a>
31</td>
32<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>
33
34<p>The QDict class is a template class that provides a
35dictionary based on QString keys.
36<a href="#details">More...</a>
37<p><tt>#include &lt;<a href="qdict-h.html">qdict.h</a>&gt;</tt>
38<p>Inherits <a href="qptrcollection.html">QPtrCollection</a>.
39<p><a href="qdict-members.html">List of all member functions.</a>
40<h2>Public Members</h2>
41<ul>
42<li class=fn><a href="#QDict"><b>QDict</b></a> ( int&nbsp;size = 17, bool&nbsp;caseSensitive = TRUE )</li>
43<li class=fn><a href="#QDict-2"><b>QDict</b></a> ( const&nbsp;QDict&lt;type&gt;&nbsp;&amp;&nbsp;dict )</li>
44<li class=fn><a href="#~QDict"><b>~QDict</b></a> ()</li>
45<li class=fn>QDict&lt;type&gt; &amp; <a href="#operator-eq"><b>operator=</b></a> ( const&nbsp;QDict&lt;type&gt;&nbsp;&amp;&nbsp;dict )</li>
46<li class=fn>virtual uint <a href="#count"><b>count</b></a> () const</li>
47<li class=fn>uint <a href="#size"><b>size</b></a> () const</li>
48<li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
49<li class=fn>void <a href="#insert"><b>insert</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )</li>
50<li class=fn>void <a href="#replace"><b>replace</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )</li>
51<li class=fn>bool <a href="#remove"><b>remove</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;key )</li>
52<li class=fn>type * <a href="#take"><b>take</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;key )</li>
53<li class=fn>type * <a href="#find"><b>find</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;key ) const</li>
54<li class=fn>type * <a href="#operator[]"><b>operator[]</b></a> ( const&nbsp;QString&nbsp;&amp;&nbsp;key ) const</li>
55<li class=fn>virtual void <a href="#clear"><b>clear</b></a> ()</li>
56<li class=fn>void <a href="#resize"><b>resize</b></a> ( uint&nbsp;newsize )</li>
57<li class=fn>void <a href="#statistics"><b>statistics</b></a> () const</li>
58</ul>
59<h2>Important Inherited Members</h2>
60<ul>
61<li class=fn>bool <a href="#autoDelete"><b>autoDelete</b></a> () const</li>
62<li class=fn>void <a href="#setAutoDelete"><b>setAutoDelete</b></a> ( bool&nbsp;enable )</li>
63</ul>
64<h2>Protected Members</h2>
65<ul>
66<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>
67<li class=fn>virtual QDataStream &amp; <a href="#write"><b>write</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QPtrCollection::Item ) const</li>
68</ul>
69<hr><a name="details"></a><h2>Detailed Description</h2>
70
71
72The QDict class is a template class that provides a
73dictionary based on <a href="qstring.html">QString</a> keys.
74<p>
75
76
77<p>
78<p> <a href="qmap.html">QMap</a> is an STL-compatible alternative to this class.
79<p> QDict is implemented as a template class. Define a template
80instance QDict&lt;X&gt; to create a dictionary that operates on
81pointers to X (X *).
82<p> A dictionary is a collection of key-value pairs. The key is a
83QString used for insertion, removal and lookup. The value is a
84pointer. Dictionaries provide very fast insertion and lookup.
85<p> If you want to use non-Unicode, plain 8-bit <tt>char*</tt> keys, use the
86<a href="qasciidict.html">QAsciiDict</a> template. A QDict has the same performance as a
87QAsciiDict. If you want to have a dictionary that maps QStrings to
88QStrings use QMap.
89<p> The <a href="#size">size</a>() of the dictionary is very important. In order to get
90good performance, you should use a suitably large <a href="primes.html#prime">prime</a> number.
91Suitable means equal to or larger than the maximum expected number
92of dictionary items. Size is set in the constructor but may be
93changed with <a href="#resize">resize</a>().
94<p> Items are inserted with <a href="#insert">insert</a>(); 0 pointers cannot be inserted.
95Items are removed with <a href="#remove">remove</a>(). All the items in a dictionary can
96be removed with <a href="#clear">clear</a>(). The number of items in the dictionary is
97returned by <a href="#count">count</a>(). If the dictionary contains no items <a href="#isEmpty">isEmpty</a>()
98returns TRUE. You can change an item's value with <a href="#replace">replace</a>(). Items
99are looked up with <a href="#operator[]">operator[]</a>(), or with <a href="#find">find</a>() which return a
100pointer to the value or 0 if the given key does not exist. You can
101take an item out of the dictionary with <a href="#take">take</a>().
102<p> Calling <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>(TRUE) for a dictionary tells it to delete
103items that are removed. The default behaviour is not to delete
104items when they are removed.
105<p> When an item is inserted, the key is converted (hashed) to an
106integer index into an internal hash array. This makes lookup very
107fast.
108<p> Items with equal keys are allowed. When inserting two items with
109the same key, only the last inserted item will be accessible (last
110in, first out) until it is removed.
111<p> The <a href="qdictiterator.html">QDictIterator</a> class can traverse the dictionary, but only in
112an arbitrary order. Multiple iterators may independently traverse
113the same dictionary.
114<p> When inserting an item into a dictionary, only the pointer is
115copied, not the item itself, i.e. a <a href="shclass.html#shallow-copy">shallow copy</a> is made. It is
116possible to make the dictionary copy all of the item's data (a
117<a href="shclass.html#deep-copy">deep copy</a>) when an item is inserted. <a href="#insert">insert</a>() calls the virtual
118function <a href="qptrcollection.html#newItem">QPtrCollection::newItem</a>() for the item to be inserted.
119Inherit a dictionary and reimplement <a href="qptrcollection.html#newItem">newItem</a>() if you want deep
120copies.
121<p> When removing a dictionary item, the virtual function
122<a href="qptrcollection.html#deleteItem">QPtrCollection::deleteItem</a>() is called. QDict's default
123implementation is to delete the item if auto-deletion is enabled.
124<p> Example #1:
125<pre>
126 QDict&lt;QLineEdit&gt; fields; // QString keys, QLineEdit* values
127 fields.<a href="#insert">insert</a>( "forename", new <a href="qlineedit.html">QLineEdit</a>( this ) );
128 fields.<a href="#insert">insert</a>( "surname", new <a href="qlineedit.html">QLineEdit</a>( this ) );
129
130 fields["forename"]-&gt;setText( "Homer" );
131 fields["surname"]-&gt;setText( "Simpson" );
132
133 <a href="qdictiterator.html">QDictIterator</a>&lt;QLineEdit&gt; it( fields ); // See QDictIterator
134 for( ; it.<a href="qdictiterator.html#current">current</a>(); ++it )
135 cout &lt;&lt; it.<a href="qdictiterator.html#currentKey">currentKey</a>() &lt;&lt; ": " &lt;&lt; it.<a href="qdictiterator.html#current">current</a>()-&gt;text() &lt;&lt; endl;
136 cout &lt;&lt; endl;
137
138 if ( fields["forename"] &amp;&amp; fields["surname"] )
139 cout &lt;&lt; fields["forename"]-&gt;text() &lt;&lt; " "
140 &lt;&lt; fields["surname"]-&gt;text() &lt;&lt; endl; // Prints "Homer Simpson"
141
142 fields.<a href="#remove">remove</a>( "forename" ); // Does not delete the line edit
143 if ( ! fields["forename"] )
144 cout &lt;&lt; "forename is not in the dictionary" &lt;&lt; endl;
145 </pre>
146
147In this example we use a dictionary to keep track of the line
148edits we're using. We insert each line edit into the dictionary
149with a unique name and then access the line edits via the
150dictionary.
151<p> Example #2:
152<pre>
153 <a href="qstringlist.html">QStringList</a> styleList = QStyleFactory::styles();
154 styleList.<a href="qstringlist.html#sort">sort</a>();
155 QDict&lt;int&gt; letterDict( 17, FALSE );
156 for ( QStringList::Iterator it = styleList.<a href="qvaluelist.html#begin">begin</a>(); it != styleList.<a href="qvaluelist.html#end">end</a>(); ++it ) {
157 <a href="qstring.html">QString</a> styleName = *it;
158 <a href="qstring.html">QString</a> styleAccel = styleName;
159 if ( letterDict[styleAccel.<a href="qstring.html#left">left</a>(1)] ) {
160 for ( uint i = 0; i &lt; styleAccel.<a href="qstring.html#length">length</a>(); i++ ) {
161 if ( ! letterDict[styleAccel.<a href="qstring.html#mid">mid</a>( i, 1 )] ) {
162 styleAccel = styleAccel.<a href="qstring.html#insert">insert</a>( i, '&amp;' );
163 letterDict.<a href="#insert">insert</a>(styleAccel.<a href="qstring.html#mid">mid</a>( i, 1 ), (const int *)1);
164 break;
165 }
166 }
167 } else {
168 styleAccel = "&amp;" + styleAccel;
169 letterDict.<a href="#insert">insert</a>(styleAccel.<a href="qstring.html#left">left</a>(1), (const int *)1);
170 }
171 (void) new <a href="qaction.html">QAction</a>( styleName, QIconSet(), styleAccel, parent );
172 }
173 </pre>
174
175In the example we are using the dictionary to provide fast random
176access to the keys, and we don't care what the values are. The
177example is used to generate a menu of QStyles, each with a unique
178accelerator key (or no accelerator if there are no unused letters
179left).
180<p> We first obtain the list of available styles, then sort them so
181that the menu items will be ordered alphabetically. Next we create
182a dictionary of int pointers. The keys in the dictionary are each
183one character long, representing letters that have been used for
184accelerators. We iterate through our list of style names. If the
185first letter of the style name is in the dictionary, i.e. has been
186used, we iterate over all the characters in the style name to see
187if we can find a letter that hasn't been used. If we find an
188unused letter we put the accelerator ampersand (&) in front of it
189and add that letter to the dictionary. If we can't find an unused
190letter the style will simply have no accelerator. If the first
191letter of the style name is not in the dictionary we use it for
192the accelerator and add it to the dictionary. Finally we create a
193<a href="qaction.html">QAction</a> for each style.
194<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>.
195
196<hr><h2>Member Function Documentation</h2>
197<h3 class=fn><a name="QDict"></a>QDict::QDict ( int&nbsp;size = 17, bool&nbsp;caseSensitive = TRUE )
198</h3>
199
200<p> Constructs a dictionary optimized for less than <em>size</em> entries.
201<p> We recommend setting <em>size</em> to a suitably large <a href="primes.html#prime">prime</a> number
202(e.g. a prime that's slightly larger than the expected number of
203entries). This makes the hash distribution better which will lead
204to faster lookup.
205<p> If <em>caseSensitive</em> is TRUE (the default), keys which differ only
206by case are considered different.
207
208<h3 class=fn><a name="QDict-2"></a>QDict::QDict ( const&nbsp;<a href="qdict.html">QDict</a>&lt;type&gt;&nbsp;&amp;&nbsp;dict )
209</h3>
210
211<p> Constructs a copy of <em>dict</em>.
212<p> Each item in <em>dict</em> is inserted into this dictionary. Only the
213pointers are copied (shallow copy).
214
215<h3 class=fn><a name="~QDict"></a>QDict::~QDict ()
216</h3>
217
218<p> Removes all items from the dictionary and destroys it. If
219<a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>() is TRUE, each value is deleted. All iterators that
220access this dictionary will be reset.
221<p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
222
223<h3 class=fn>bool <a name="autoDelete"></a>QPtrCollection::autoDelete () const
224</h3>
225
226<p> Returns the setting of the auto-delete option. The default is FALSE.
227<p> <p>See also <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
228
229<h3 class=fn>void <a name="clear"></a>QDict::clear ()<tt> [virtual]</tt>
230</h3>
231
232<p> Removes all items from the dictionary.
233<p> The removed items are deleted if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.
234<p> All dictionary iterators that operate on the dictionary are reset.
235<p> <p>See also <a href="#remove">remove</a>(), <a href="#take">take</a>(), and <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
236
237<p>Reimplemented from <a href="qptrcollection.html#clear">QPtrCollection</a>.
238<h3 class=fn>uint <a name="count"></a>QDict::count () const<tt> [virtual]</tt>
239</h3>
240
241<p> Returns the number of items in the dictionary.
242<p> <p>See also <a href="#isEmpty">isEmpty</a>().
243
244<p>Reimplemented from <a href="qptrcollection.html#count">QPtrCollection</a>.
245<h3 class=fn>type * <a name="find"></a>QDict::find ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;key ) const
246</h3>
247
248<p> Returns the item with key <em>key</em>, or 0 if the key does not exist
249in the dictionary.
250<p> If there are two or more items with equal keys, then the most
251recently inserted item will be found.
252<p> Equivalent to the [] operator.
253<p> <p>See also <a href="#operator[]">operator[]</a>().
254
255<h3 class=fn>void <a name="insert"></a>QDict::insert ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )
256</h3>
257
258<p> Inserts the key <em>key</em> with value <em>item</em> into the dictionary.
259<p> Multiple items can have the same key, in which case only the last
260item will be accessible using <a href="#operator[]">operator[]</a>().
261<p> <em>item</em> may not be 0.
262<p> <p>See also <a href="#replace">replace</a>().
263
264<p>Example: <a href="themes-example.html#x326">themes/themes.cpp</a>.
265<h3 class=fn>bool <a name="isEmpty"></a>QDict::isEmpty () const
266</h3>
267
268<p> Returns TRUE if the dictionary is empty, i.e. <a href="#count">count</a>() == 0;
269otherwise returns FALSE.
270<p> <p>See also <a href="#count">count</a>().
271
272<h3 class=fn><a href="qdict.html">QDict</a>&lt;type&gt;&nbsp;&amp; <a name="operator-eq"></a>QDict::operator= ( const&nbsp;<a href="qdict.html">QDict</a>&lt;type&gt;&nbsp;&amp;&nbsp;dict )
273</h3>
274
275<p> Assigns <em>dict</em> to this dictionary and returns a reference to this
276dictionary.
277<p> This dictionary is first cleared, then each item in <em>dict</em> is
278inserted into this dictionary. Only the pointers are copied
279(shallow copy), unless <a href="qptrcollection.html#newItem">newItem</a>() has been reimplemented.
280
281<h3 class=fn>type * <a name="operator[]"></a>QDict::operator[] ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;key ) const
282</h3>
283
284<p> Returns the item with key <em>key</em>, or 0 if the key does not
285exist in the dictionary.
286<p> If there are two or more items with equal keys, then the most
287recently inserted item will be found.
288<p> Equivalent to the <a href="#find">find</a>() function.
289<p> <p>See also <a href="#find">find</a>().
290
291<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="read"></a>QDict::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>
292</h3>
293
294<p> Reads a dictionary item from the stream <em>s</em> and returns a
295reference to the stream.
296<p> The default implementation sets <em>item</em> to 0.
297<p> <p>See also <a href="#write">write</a>().
298
299<h3 class=fn>bool <a name="remove"></a>QDict::remove ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;key )
300</h3>
301
302<p> Removes the item with <em>key</em> from the dictionary. Returns TRUE if
303successful, i.e. if the item is in the dictionary; otherwise
304returns FALSE.
305<p> If there are two or more items with equal keys, then the last item
306that was inserted will be removed.
307<p> The removed item is deleted if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled.
308<p> All dictionary iterators that refer to the removed item will be
309set to point to the next item in the dictionary's traversal order.
310<p> <p>See also <a href="#take">take</a>(), <a href="#clear">clear</a>(), and <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
311
312<h3 class=fn>void <a name="replace"></a>QDict::replace ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;key, const&nbsp;type&nbsp;*&nbsp;item )
313</h3>
314
315<p> Replaces the value of the key, <em>key</em> with <em>item</em>.
316<p> If the item does not already exist, it will be inserted.
317<p> <em>item</em> may not be 0.
318<p> Equivalent to:
319<pre>
320 QDict&lt;char&gt; dict;
321 ...
322 if ( dict.<a href="#find">find</a>( key ) )
323 dict.<a href="#remove">remove</a>( key );
324 dict.<a href="#insert">insert</a>( key, item );
325 </pre>
326
327<p> If there are two or more items with equal keys, then the last item
328that was inserted will be replaced.
329<p> <p>See also <a href="#insert">insert</a>().
330
331<h3 class=fn>void <a name="resize"></a>QDict::resize ( uint&nbsp;newsize )
332</h3>
333
334<p> Changes the size of the hash table to <em>newsize</em>. The contents of
335the dictionary are preserved, but all iterators on the dictionary
336become invalid.
337
338<h3 class=fn>void <a name="setAutoDelete"></a>QPtrCollection::setAutoDelete ( bool&nbsp;enable )
339</h3>
340
341<p> Sets the collection to auto-delete its contents if <em>enable</em> is
342TRUE and to never delete them if <em>enable</em> is FALSE.
343<p> If auto-deleting is turned on, all the items in a collection are
344deleted when the collection itself is deleted. This is convenient
345if the collection has the only pointer to the items.
346<p> The default setting is FALSE, for safety. If you turn it on, be
347careful about copying the collection - you might find yourself
348with two collections deleting the same items.
349<p> Note that the auto-delete setting may also affect other functions
350in subclasses. For example, a subclass that has a <a href="#remove">remove</a>()
351function will remove the item from its data structure, and if
352auto-delete is enabled, will also delete the item.
353<p> <p>See also <a href="qptrcollection.html#autoDelete">autoDelete</a>().
354
355<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>.
356<h3 class=fn>uint <a name="size"></a>QDict::size () const
357</h3>
358
359<p> Returns the size of the internal hash array (as specified in the
360constructor).
361<p> <p>See also <a href="#count">count</a>().
362
363<h3 class=fn>void <a name="statistics"></a>QDict::statistics () const
364</h3>
365
366<p> Debugging-only function that prints out the dictionary
367distribution using <a href="qapplication.html#qDebug">qDebug</a>().
368
369<h3 class=fn>type * <a name="take"></a>QDict::take ( const&nbsp;<a href="qstring.html">QString</a>&nbsp;&amp;&nbsp;key )
370</h3>
371
372<p> Takes the item with <em>key</em> out of the dictionary without deleting
373it (even if <a href="qptrcollection.html#setAutoDelete">auto-deletion</a> is enabled).
374<p> If there are two or more items with equal keys, then the last item
375that was inserted will be taken.
376<p> Returns a pointer to the item taken out, or 0 if the key does not
377exist in the dictionary.
378<p> All dictionary iterators that refer to the taken item will be set
379to point to the next item in the dictionary traversal order.
380<p> <p>See also <a href="#remove">remove</a>(), <a href="#clear">clear</a>(), and <a href="qptrcollection.html#setAutoDelete">setAutoDelete</a>().
381
382<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="write"></a>QDict::write ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, <a href="qptrcollection.html#Item">QPtrCollection::Item</a> ) const<tt> [virtual protected]</tt>
383</h3>
384
385<p> Writes a dictionary item to the stream <em>s</em> and returns a
386reference to the stream.
387<p> <p>See also <a href="#read">read</a>().
388
389<!-- eof -->
390<hr><p>
391This file is part of the <a href="index.html">Qt toolkit</a>.
392Copyright &copy; 1995-2007
393<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
394<table width=100% cellspacing=0 border=0><tr>
395<td>Copyright &copy; 2007
396<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
397<td align=right><div align=right>Qt 3.3.8</div>
398</table></div></address></body>
399</html>
Note: See TracBrowser for help on using the repository browser.