source: trunk/doc/html/qmap.html@ 203

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

reference documentation added

File size: 28.7 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/qmap.doc:41 -->
3<html>
4<head>
5<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
6<title>QMap 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>QMap Class Reference</h1>
33
34<p>The QMap class is a value-based template class that
35provides a dictionary.
36<a href="#details">More...</a>
37<p><tt>#include &lt;<a href="qmap-h.html">qmap.h</a>&gt;</tt>
38<p><a href="qmap-members.html">List of all member functions.</a>
39<h2>Public Members</h2>
40<ul>
41<li class=fn>typedef Key&nbsp;<a href="#key_type"><b>key_type</b></a></li>
42<li class=fn>typedef T&nbsp;<a href="#mapped_type"><b>mapped_type</b></a></li>
43<li class=fn>typedef QPair&lt;const&nbsp;key_type,&nbsp;mapped_type&gt;&nbsp;<a href="#value_type"><b>value_type</b></a></li>
44<li class=fn>typedef value_type&nbsp;*&nbsp;<a href="#pointer"><b>pointer</b></a></li>
45<li class=fn>typedef const&nbsp;value_type&nbsp;*&nbsp;<a href="#const_pointer"><b>const_pointer</b></a></li>
46<li class=fn>typedef value_type&nbsp;&amp;&nbsp;<a href="#reference"><b>reference</b></a></li>
47<li class=fn>typedef const&nbsp;value_type&nbsp;&amp;&nbsp;<a href="#const_reference"><b>const_reference</b></a></li>
48<li class=fn>typedef size_t&nbsp;<a href="#size_type"><b>size_type</b></a></li>
49<li class=fn>typedef QMapIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#iterator"><b>iterator</b></a></li>
50<li class=fn>typedef QMapConstIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#const_iterator"><b>const_iterator</b></a></li>
51<li class=fn>typedef QPair&lt;iterator,&nbsp;bool&gt;&nbsp;<b>insert_pair</b></li>
52<li class=fn>typedef QMapIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#Iterator"><b>Iterator</b></a></li>
53<li class=fn>typedef QMapConstIterator&lt;Key,&nbsp;T&gt;&nbsp;<a href="#ConstIterator"><b>ConstIterator</b></a></li>
54<li class=fn>typedef T&nbsp;<a href="#ValueType"><b>ValueType</b></a></li>
55<li class=fn><a href="#QMap"><b>QMap</b></a> ()</li>
56<li class=fn><a href="#QMap-2"><b>QMap</b></a> ( const&nbsp;QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</li>
57<li class=fn><a href="#QMap-3"><b>QMap</b></a> ( const&nbsp;std::map&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</li>
58<li class=fn><a href="#~QMap"><b>~QMap</b></a> ()</li>
59<li class=fn>QMap&lt;Key, T&gt; &amp; <a href="#operator-eq"><b>operator=</b></a> ( const&nbsp;QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</li>
60<li class=fn>QMap&lt;Key, T&gt; &amp; <a href="#operator-eq-2"><b>operator=</b></a> ( const&nbsp;std::map&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</li>
61<li class=fn>iterator <a href="#begin"><b>begin</b></a> ()</li>
62<li class=fn>iterator <a href="#end"><b>end</b></a> ()</li>
63<li class=fn>const_iterator <a href="#begin-2"><b>begin</b></a> () const</li>
64<li class=fn>const_iterator <a href="#end-2"><b>end</b></a> () const</li>
65<li class=fn>const_iterator <a href="#constBegin"><b>constBegin</b></a> () const</li>
66<li class=fn>const_iterator <a href="#constEnd"><b>constEnd</b></a> () const</li>
67<li class=fn>iterator <a href="#replace"><b>replace</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k, const&nbsp;T&nbsp;&amp;&nbsp;v )</li>
68<li class=fn>size_type <a href="#size"><b>size</b></a> () const</li>
69<li class=fn>bool <a href="#empty"><b>empty</b></a> () const</li>
70<li class=fn>QPair&lt;iterator, bool&gt; <a href="#insert-2"><b>insert</b></a> ( const&nbsp;value_type&nbsp;&amp;&nbsp;x )</li>
71<li class=fn>void <a href="#erase"><b>erase</b></a> ( iterator&nbsp;it )</li>
72<li class=fn>void <a href="#erase-2"><b>erase</b></a> ( const&nbsp;key_type&nbsp;&amp;&nbsp;k )</li>
73<li class=fn>size_type <a href="#count"><b>count</b></a> ( const&nbsp;key_type&nbsp;&amp;&nbsp;k ) const</li>
74<li class=fn>T &amp; <a href="#operator[]"><b>operator[]</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k )</li>
75<li class=fn>void <a href="#clear"><b>clear</b></a> ()</li>
76<li class=fn>iterator <a href="#find"><b>find</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k )</li>
77<li class=fn>const_iterator <a href="#find-2"><b>find</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const</li>
78<li class=fn>const T &amp; <a href="#operator[]-2"><b>operator[]</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const</li>
79<li class=fn>bool <a href="#contains"><b>contains</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const</li>
80<li class=fn>size_type <a href="#count-2"><b>count</b></a> () const</li>
81<li class=fn>QValueList&lt;Key&gt; <a href="#keys"><b>keys</b></a> () const</li>
82<li class=fn>QValueList&lt;T&gt; <a href="#values"><b>values</b></a> () const</li>
83<li class=fn>bool <a href="#isEmpty"><b>isEmpty</b></a> () const</li>
84<li class=fn>iterator <a href="#insert"><b>insert</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;key, const&nbsp;T&nbsp;&amp;&nbsp;value, bool&nbsp;overwrite = TRUE )</li>
85<li class=fn>void <a href="#remove"><b>remove</b></a> ( iterator&nbsp;it )</li>
86<li class=fn>void <a href="#remove-2"><b>remove</b></a> ( const&nbsp;Key&nbsp;&amp;&nbsp;k )</li>
87</ul>
88<h2>Protected Members</h2>
89<ul>
90<li class=fn>void <a href="#detach"><b>detach</b></a> ()</li>
91</ul>
92<h2>Related Functions</h2>
93<ul>
94<li class=fn>QDataStream &amp; <a href="#operator-gt-gt"><b>operator&gt;&gt;</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</li>
95<li class=fn>QDataStream &amp; <a href="#operator-lt-lt"><b>operator&lt;&lt;</b></a> ( QDataStream&nbsp;&amp;&nbsp;s, const&nbsp;QMap&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )</li>
96</ul>
97<hr><a name="details"></a><h2>Detailed Description</h2>
98
99
100The QMap class is a value-based template class that
101provides a dictionary.
102<p>
103
104
105
106<p> QMap is a Qt implementation of an STL-like map container. It can
107be used in your application if the standard <tt>map</tt> is not
108available on all your target platforms. QMap is part of the <a href="qtl.html">Qt Template Library</a>.
109<p> QMap&lt;Key, Data&gt; defines a template instance to create a
110dictionary with keys of type Key and values of type Data. QMap
111does not store pointers to the members of the map; instead, it
112holds a copy of every member. For this reason, QMap is
113value-based, whereas <a href="qptrlist.html">QPtrList</a> and <a href="qdict.html">QDict</a> are pointer-based.
114<p> QMap contains and manages a collection of objects of type Data
115with associated key values of type Key and provides iterators that
116allow the contained objects to be addressed. QMap owns the
117contained items.
118<p> Some classes cannot be used within a QMap. For example everything
119derived from <a href="qobject.html">QObject</a> and thus all classes that implement widgets.
120Only values can be used in a QMap. To qualify as a value, the
121class must provide
122<p> <ul>
123<li> A copy constructor
124<li> An assignment operator
125<li> A default constructor, i.e. a constructor that does not take
126any arguments.
127</ul>
128<p> Note that C++ defaults to field-by-field assignment operators and
129copy constructors if no explicit version is supplied. In many
130cases, this is sufficient.
131<p> The class used for the key requires that the <tt>operator&lt;</tt> is
132implemented to define ordering of the keys.
133<p> QMap's function naming is consistent with the other Qt classes
134(e.g., <a href="#count">count</a>(), <a href="#isEmpty">isEmpty</a>()). QMap also provides extra functions for
135compatibility with STL algorithms, such as <a href="#size">size</a>() and <a href="#empty">empty</a>().
136Programmers already familiar with the STL <tt>map</tt> can use these
137the STL-like functions if preferred.
138<p> Example:
139<a name="qmap-eg"></a>
140<pre>
141 #include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
142 #include &lt;<a href="qmap-h.html">qmap.h</a>&gt;
143 #include &lt;<a href="qstring-h.html">qstring.h</a>&gt;
144
145 class Employee
146 {
147 public:
148 Employee(): sn(0) {}
149 Employee( const <a href="qstring.html">QString</a>&amp; forename, const <a href="qstring.html">QString</a>&amp; surname, int salary )
150 : fn(forename), sn(surname), sal(salary)
151 { }
152
153 <a href="qstring.html">QString</a> forename() const { return fn; }
154 <a href="qstring.html">QString</a> surname() const { return sn; }
155 int salary() const { return sal; }
156 void setSalary( int salary ) { sal = salary; }
157
158 private:
159 <a href="qstring.html">QString</a> fn;
160 <a href="qstring.html">QString</a> sn;
161 int sal;
162 };
163
164 int main(int argc, char **argv)
165 {
166 <a href="qapplication.html">QApplication</a> app( argc, argv );
167
168 typedef QMap&lt;QString, Employee&gt; EmployeeMap;
169 EmployeeMap map;
170
171 map["JD001"] = Employee("John", "Doe", 50000);
172 map["JW002"] = Employee("Jane", "Williams", 80000);
173 map["TJ001"] = Employee("Tom", "Jones", 60000);
174
175 Employee sasha( "Sasha", "Hind", 50000 );
176 map["SH001"] = sasha;
177 sasha.setSalary( 40000 );
178
179 EmployeeMap::<a href="#Iterator">Iterator</a> it;
180 for ( it = map.begin(); it != map.end(); ++it ) {
181 printf( "%s: %s, %s earns %d\n",
182 it.key().latin1(),
183 it.data().surname().latin1(),
184 it.data().forename().latin1(),
185 it.data().salary() );
186 }
187 return 0;
188 }
189 </pre>
190
191<p> Program output:
192<pre>
193 JD001: Doe, John earns 50000
194 JW002: Williams, Jane earns 80000
195 SH001: Hind, Sasha earns 50000
196 TJ001: Jones, Tom earns 60000
197 </pre>
198
199<p> The latest changes to Sasha's salary did not affect the value in
200the list because the map created a copy of Sasha's entry. In
201addition, notice that the items are sorted alphabetically (by key)
202when iterating over the map.
203<p> There are several ways to find items in a map. The <a href="#begin">begin</a>() and
204<a href="#end">end</a>() functions return iterators to the beginning and end of the
205map. The advantage of using an iterator is that you can move
206forward or backward by incrementing/decrementing the iterator.
207The iterator returned by end() points to the element which is one
208past the last element in the container. The past-the-end iterator
209is still associated with the map it belongs to, however it is <em>not</em> dereferenceable; <a href="qsize.html#operator*-4">operator*</a>() will not return a well-defined
210value. If the map is empty, the iterator returned by begin() will
211equal the iterator returned by end().
212<p> Another way to find an element in the map is by using the <a href="#find">find</a>()
213function. This returns an iterator pointing to the desired item or
214to the end() iterator if no such element exists.
215<p> Another approach uses the operator[]. But be warned: if the map
216does not contain an entry for the element you are looking for,
217operator[] inserts a default value. If you do not know that the
218element you are searching for is really in the list, you should
219not use operator[]. The following example illustrates this:
220<p> <pre>
221 QMap&lt;QString,QString&gt; map;
222 map["Clinton"] = "Bill";
223 str &lt;&lt; map["Clinton"] &lt;&lt; map["Bush"] &lt;&lt; endl;
224 </pre>
225
226<p> The code fragment will print out "Clinton", "". Since the value
227associated with the "Bush" key did not exist, the map inserted a
228default value (in this case, an empty string). If you are not
229sure whether a certain element is in the map, you should use
230find() and iterators instead.
231<p> If you just want to know whether a certain key is contained in the
232map, use the <a href="#contains">contains</a>() function. In addition, <a href="#count">count</a>() tells you
233how many keys are in the map.
234<p> It is safe to have multiple iterators at the same time. If some
235member of the map is removed, only iterators pointing to the
236removed member become invalid; inserting in the map does not
237invalidate any iterators.
238<p> Since QMap is value-based, there is no need to be concerned about
239deleting items in the map. The map holds its own copies and will
240free them if the corresponding member or the map itself is
241deleted.
242<p> QMap is <a href="shclass.html#implicitly-shared">implicitly shared</a>. This means you can just make copies of
243the map in time O(1). If multiple QMap instances share the same
244data and one is modifying the map's data, this modifying instance
245makes a copy and modifies its private copy: so it does not affect
246other instances. If a QMap is being used in a multi-threaded
247program, you must protect all access to the map. See <a href="qmutex.html">QMutex</a>.
248<p> There are a couple of ways of inserting new items into the map.
249One uses the <a href="#insert">insert</a>() method; the other uses operator[]:
250<pre>
251 QMap&lt;QString, QString&gt; map;
252 map["Clinton"] = "Bill";
253 map.<a href="#insert">insert</a>( "Bush", "George" );
254 </pre>
255
256<p> Items can also be removed from the map in several ways. One way is
257to pass an iterator to <a href="#remove">remove</a>(). Another way is to pass a key
258value to remove(), which will delete the entry with the requested
259key. In addition you can clear the entire map using the <a href="#clear">clear</a>()
260method.
261<p> <p>See also <a href="qmapiterator.html">QMapIterator</a>, <a href="qtl.html">Qt Template Library Classes</a>, <a href="shared.html">Implicitly and Explicitly Shared Classes</a>, and <a href="tools.html">Non-GUI Classes</a>.
262
263<hr><h2>Member Type Documentation</h2>
264<h3 class=fn><a name="ConstIterator"></a>QMap::ConstIterator</h3>
265The map's const iterator type, Qt style.
266<h3 class=fn><a name="Iterator"></a>QMap::Iterator</h3>
267The map's iterator type, Qt style.
268<h3 class=fn><a name="ValueType"></a>QMap::ValueType</h3>
269Corresponds to <a href="qpair.html">QPair</a>&lt;key_type, mapped_type&gt;, Qt style.
270<h3 class=fn><a name="const_iterator"></a>QMap::const_iterator</h3>
271The map's const iterator type.
272<h3 class=fn><a name="const_pointer"></a>QMap::const_pointer</h3>
273Const pointer to value_type.
274<h3 class=fn><a name="const_reference"></a>QMap::const_reference</h3>
275Const reference to value_type.
276<h3 class=fn><a name="iterator"></a>QMap::iterator</h3>
277The map's iterator type.
278<h3 class=fn><a name="key_type"></a>QMap::key_type</h3>
279The map's key type.
280<h3 class=fn><a name="mapped_type"></a>QMap::mapped_type</h3>
281The map's data type.
282<h3 class=fn><a name="pointer"></a>QMap::pointer</h3>
283Pointer to value_type.
284<h3 class=fn><a name="reference"></a>QMap::reference</h3>
285Reference to value_type.
286<h3 class=fn><a name="size_type"></a>QMap::size_type</h3>
287An unsigned integral type, used to represent various sizes.
288<h3 class=fn><a name="value_type"></a>QMap::value_type</h3>
289Corresponds to <a href="qpair.html">QPair</a>&lt;key_type, mapped_type&gt;.
290<hr><h2>Member Function Documentation</h2>
291<h3 class=fn><a name="QMap"></a>QMap::QMap ()
292</h3>
293
294<p> Constructs an empty map.
295
296<h3 class=fn><a name="QMap-2"></a>QMap::QMap ( const&nbsp;<a href="qmap.html">QMap</a>&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )
297</h3>
298
299<p> Constructs a copy of <em>m</em>.
300<p> This operation costs O(1) time because QMap is <a href="shclass.html#implicitly-shared">implicitly shared</a>.
301This makes returning a QMap from a function very fast. If a shared
302instance is modified, it will be copied (copy-on-write), and this
303takes O(n) time.
304
305<h3 class=fn><a name="QMap-3"></a>QMap::QMap ( const&nbsp;std::map&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )
306</h3>
307
308<p> Constructs a copy of <em>m</em>.
309
310<h3 class=fn><a name="~QMap"></a>QMap::~QMap ()
311</h3>
312
313<p> Destroys the map. References to the values in the map and all
314iterators of this map become invalidated. Since QMap is highly
315tuned for performance you won't see warnings if you use invalid
316iterators, because it is not possible for an iterator to check
317whether it is valid or not.
318
319<h3 class=fn><a href="qmap.html#iterator">iterator</a> <a name="begin"></a>QMap::begin ()
320</h3>
321
322<p> Returns an iterator pointing to the first element in the map. This
323iterator equals <a href="#end">end</a>() if the map is empty.
324<p> The items in the map are traversed in the order defined by
325operator&lt;(Key, Key).
326<p> <p>See also <a href="#end">end</a>() and <a href="qmapiterator.html">QMapIterator</a>.
327
328<h3 class=fn><a href="qmap.html#const_iterator">const_iterator</a> <a name="begin-2"></a>QMap::begin () const
329</h3>
330
331<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
332<p> <p>See also <a href="#end">end</a>() and <a href="qmapconstiterator.html">QMapConstIterator</a>.
333
334<h3 class=fn>void <a name="clear"></a>QMap::clear ()
335</h3>
336
337<p> Removes all items from the map.
338<p> <p>See also <a href="#remove">remove</a>().
339
340<h3 class=fn><a href="qmap.html#const_iterator">const_iterator</a> <a name="constBegin"></a>QMap::constBegin () const
341</h3>
342
343<p> Returns an iterator pointing to the first element in the map. This
344iterator equals <a href="#end">end</a>() if the map is empty.
345<p> The items in the map are traversed in the order defined by
346operator&lt;(Key, Key).
347<p> <p>See also <a href="#constEnd">constEnd</a>() and <a href="qmapconstiterator.html">QMapConstIterator</a>.
348
349<h3 class=fn><a href="qmap.html#const_iterator">const_iterator</a> <a name="constEnd"></a>QMap::constEnd () const
350</h3>
351
352<p> The iterator returned by <a href="#end">end</a>() points to the element which is one
353past the last element in the container. The past-the-end iterator
354is still associated with the map it belongs to, but it is <em>not</em>
355dereferenceable; <a href="qsize.html#operator*-4">operator*</a>() will not return a well-defined value.
356<p> This iterator equals <a href="#constBegin">constBegin</a>() if the map is empty.
357<p> <p>See also <a href="#constBegin">constBegin</a>() and <a href="qmapconstiterator.html">QMapConstIterator</a>.
358
359<h3 class=fn>bool <a name="contains"></a>QMap::contains ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const
360</h3>
361
362<p> Returns TRUE if the map contains an item with key <em>k</em>; otherwise
363returns FALSE.
364
365<h3 class=fn><a href="qmap.html#size_type">size_type</a> <a name="count"></a>QMap::count ( const&nbsp;<a href="qmap.html#key_type">key_type</a>&nbsp;&amp;&nbsp;k ) const
366</h3>
367
368<p> Returns the number of items whose key is <em>k</em>. Since QMap does not
369allow duplicate keys, the return value is always 0 or 1.
370<p> This function is provided for STL compatibility.
371
372<h3 class=fn><a href="qmap.html#size_type">size_type</a> <a name="count-2"></a>QMap::count () const
373</h3>
374
375<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
376<p> Returns the number of items in the map.
377<p> <p>See also <a href="#isEmpty">isEmpty</a>().
378
379<h3 class=fn>void <a name="detach"></a>QMap::detach ()<tt> [protected]</tt>
380</h3>
381
382<p> If the map does not share its data with another QMap instance,
383nothing happens; otherwise the function creates a new copy of this
384map and detaches from the shared one. This function is called
385whenever the map is modified. The <a href="shclass.html#implicit-sharing">implicit sharing</a> mechanism is
386implemented this way.
387
388<h3 class=fn>bool <a name="empty"></a>QMap::empty () const
389</h3>
390
391<p> Returns TRUE if the map contains no items; otherwise returns
392FALSE.
393<p> This function is provided for STL compatibility. It is equivalent
394to <a href="#isEmpty">isEmpty</a>().
395<p> <p>See also <a href="#size">size</a>().
396
397<h3 class=fn><a href="qmap.html#iterator">iterator</a> <a name="end"></a>QMap::end ()
398</h3>
399
400<p> The iterator returned by <a href="#end">end</a>() points to the element which is one
401past the last element in the container. The past-the-end iterator
402is still associated with the map it belongs to, but it is <em>not</em>
403dereferenceable; <a href="qsize.html#operator*-4">operator*</a>() will not return a well-defined value.
404<p> This iterator equals <a href="#begin">begin</a>() if the map is empty.
405<p> <p>See also <a href="#begin">begin</a>() and <a href="qmapiterator.html">QMapIterator</a>.
406
407<h3 class=fn><a href="qmap.html#const_iterator">const_iterator</a> <a name="end-2"></a>QMap::end () const
408</h3>
409
410<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
411<p>
412<h3 class=fn>void <a name="erase"></a>QMap::erase ( <a href="qmap.html#iterator">iterator</a>&nbsp;it )
413</h3>
414
415<p> Removes the item associated with the iterator <em>it</em> from the map.
416<p> This function is provided for STL compatibility. It is equivalent
417to <a href="#remove">remove</a>().
418<p> <p>See also <a href="#clear">clear</a>().
419
420<h3 class=fn>void <a name="erase-2"></a>QMap::erase ( const&nbsp;<a href="qmap.html#key_type">key_type</a>&nbsp;&amp;&nbsp;k )
421</h3>
422
423<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
424<p> Removes the item with the key <em>k</em> from the map.
425
426<h3 class=fn><a href="qmap.html#iterator">iterator</a> <a name="find"></a>QMap::find ( const&nbsp;Key&nbsp;&amp;&nbsp;k )
427</h3>
428
429<p> Returns an iterator pointing to the element with key <em>k</em> in the
430map.
431<p> Returns <a href="#end">end</a>() if no key matched.
432<p> <p>See also <a href="qmapiterator.html">QMapIterator</a>.
433
434<h3 class=fn><a href="qmap.html#const_iterator">const_iterator</a> <a name="find-2"></a>QMap::find ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const
435</h3>
436
437<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
438<p> Returns an iterator pointing to the element with key <em>k</em> in the
439map.
440<p> Returns <a href="#end">end</a>() if no key matched.
441<p> <p>See also <a href="qmapconstiterator.html">QMapConstIterator</a>.
442
443<h3 class=fn><a href="qmap.html#iterator">iterator</a> <a name="insert"></a>QMap::insert ( const&nbsp;Key&nbsp;&amp;&nbsp;key, const&nbsp;T&nbsp;&amp;&nbsp;value, bool&nbsp;overwrite = TRUE )
444</h3>
445
446<p> Inserts a new item with the key, <em>key</em>, and a value of <em>value</em>.
447If there is already an item whose key is <em>key</em>, that item's value
448is replaced with <em>value</em>, unless <em>overwrite</em> is FALSE (it is
449TRUE by default). In this case an iterator to this item is
450returned, else an iterator to the new item is returned.
451<p>
452<h3 class=fn><a href="qpair.html">QPair</a>&lt;iterator,&nbsp;bool&gt; <a name="insert-2"></a>QMap::insert ( const&nbsp;<a href="qmap.html#value_type">value_type</a>&nbsp;&amp;&nbsp;x )
453</h3>
454
455<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
456<p> Inserts the (key, value) pair <em>x</em> into the map. <em>x</em> is a <a href="qpair.html">QPair</a>
457whose <tt>first</tt> element is a key to be inserted and whose <tt>second</tt>
458element is the associated value to be inserted. Returns a pair
459whose <tt>first</tt> element is an iterator pointing to the inserted
460item and whose <tt>second</tt> element is a bool indicating TRUE if <em>x</em>
461was inserted and FALSE if it was not inserted, e.g. because it was
462already present.
463<p> <p>See also <a href="#replace">replace</a>().
464
465<h3 class=fn>bool <a name="isEmpty"></a>QMap::isEmpty () const
466</h3>
467
468<p> Returns TRUE if the map contains no items; otherwise returns
469FALSE.
470<p> <p>See also <a href="#count">count</a>().
471
472<h3 class=fn><a href="qvaluelist.html">QValueList</a>&lt;Key&gt; <a name="keys"></a>QMap::keys () const
473</h3>
474
475<p> Returns a list of all the keys in the map, in order.
476
477<h3 class=fn><a href="qmap.html">QMap</a>&lt;Key,&nbsp;T&gt;&nbsp;&amp; <a name="operator-eq"></a>QMap::operator= ( const&nbsp;<a href="qmap.html">QMap</a>&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )
478</h3>
479
480<p> Assigns <em>m</em> to this map and returns a reference to this map.
481<p> All iterators of the current map become invalidated by this
482operation. The cost of such an assignment is O(1), because QMap is
483<a href="shclass.html#implicitly-shared">implicitly shared</a>.
484
485<h3 class=fn><a href="qmap.html">QMap</a>&lt;Key,&nbsp;T&gt;&nbsp;&amp; <a name="operator-eq-2"></a>QMap::operator= ( const&nbsp;std::map&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )
486</h3>
487
488<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
489<p> Assigns <em>m</em> to this map and returns a reference to this map.
490<p> All iterators of the current map become invalidated by this
491operation.
492
493<h3 class=fn>T &amp; <a name="operator[]"></a>QMap::operator[] ( const&nbsp;Key&nbsp;&amp;&nbsp;k )
494</h3>
495
496<p> Returns the value associated with the key <em>k</em>. If no such key is
497present, an empty item is inserted with this key and a reference
498to the empty item is returned.
499<p> You can use this operator both for reading and writing:
500<pre>
501 QMap&lt;QString, QString&gt; map;
502 map["Clinton"] = "Bill";
503 stream &lt;&lt; map["Clinton"];
504 </pre>
505
506
507<h3 class=fn>const T &amp; <a name="operator[]-2"></a>QMap::operator[] ( const&nbsp;Key&nbsp;&amp;&nbsp;k ) const
508</h3>
509
510<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
511<p> <b>Warning:</b> This function differs from the non-const version of the
512same function. It will <em>not</em> insert an empty value if the key <em>k</em> does not exist. This may lead to logic errors in your program.
513You should check if the element exists before calling this
514function.
515<p> Returns the value associated with the key <em>k</em>. If no such key is
516present, a reference to an empty item is returned.
517
518<h3 class=fn>void <a name="remove"></a>QMap::remove ( <a href="qmap.html#iterator">iterator</a>&nbsp;it )
519</h3>
520
521<p> Removes the item associated with the iterator <em>it</em> from the map.
522<p> <p>See also <a href="#clear">clear</a>().
523
524<h3 class=fn>void <a name="remove-2"></a>QMap::remove ( const&nbsp;Key&nbsp;&amp;&nbsp;k )
525</h3>
526
527<p> This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
528<p> Removes the item with the key <em>k</em> from the map.
529
530<h3 class=fn><a href="qmap.html#iterator">iterator</a> <a name="replace"></a>QMap::replace ( const&nbsp;Key&nbsp;&amp;&nbsp;k, const&nbsp;T&nbsp;&amp;&nbsp;v )
531</h3>
532
533<p> Replaces the value of the element with key <em>k</em>, with the value <em>v</em>.
534<p> <p>See also <a href="#insert">insert</a>() and <a href="#remove">remove</a>().
535
536<h3 class=fn><a href="qmap.html#size_type">size_type</a> <a name="size"></a>QMap::size () const
537</h3>
538
539<p> Returns the number of items in the map.
540<p> This function is provided for STL compatibility. It is equivalent
541to <a href="#count">count</a>().
542<p> <p>See also <a href="#empty">empty</a>().
543
544<h3 class=fn><a href="qvaluelist.html">QValueList</a>&lt;T&gt; <a name="values"></a>QMap::values () const
545</h3>
546
547<p> Returns a list of all the values in the map, in key order.
548
549<hr><h2>Related Functions</h2>
550<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="operator-lt-lt"></a>operator&lt;&lt; ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, const&nbsp;<a href="qmap.html">QMap</a>&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )
551</h3>
552
553<p>
554<p> Writes the map <em>m</em> to the stream <em>s</em>. The types <em>Key</em> and <em>T</em>
555must implement the streaming operator as well.
556
557<h3 class=fn><a href="qdatastream.html">QDataStream</a>&nbsp;&amp; <a name="operator-gt-gt"></a>operator&gt;&gt; ( <a href="qdatastream.html">QDataStream</a>&nbsp;&amp;&nbsp;s, <a href="qmap.html">QMap</a>&lt;Key,&nbsp;T&gt;&nbsp;&amp;&nbsp;m )
558</h3>
559
560<p>
561<p> Reads the map <em>m</em> from the stream <em>s</em>. The types <em>Key</em> and <em>T</em>
562must implement the streaming operator as well.
563
564<!-- eof -->
565<hr><p>
566This file is part of the <a href="index.html">Qt toolkit</a>.
567Copyright &copy; 1995-2007
568<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
569<table width=100% cellspacing=0 border=0><tr>
570<td>Copyright &copy; 2007
571<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
572<td align=right><div align=right>Qt 3.3.8</div>
573</table></div></address></body>
574</html>
Note: See TracBrowser for help on using the repository browser.