~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qhash-iterator.html

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2006-10-12 23:14:14 UTC
  • mto: (15.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20061012231414-y2oqbom5dy389os0
Tags: upstream-4.2.0
ImportĀ upstreamĀ versionĀ 4.2.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
<!DOCTYPE html
3
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
 
<!-- /tmp/qt-4.1.4-harald-1150713676011/qt-x11-opensource-src-4.1.4/src/corelib/tools/qhash.cpp -->
6
 
<!-- /tmp/qt-4.1.4-harald-1150713676011/qt-x11-opensource-src-4.1.4/src/corelib/tools/qhash.cpp -->
 
5
<!-- /tmp/qt-4.2.0-qt-1159540607313/qt-x11-opensource-src-4.2.0/src/corelib/tools/qhash.cpp -->
7
6
<head>
8
 
    <title>Qt 4.1: QHash::iterator Class Reference</title>
9
 
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
10
 
a:link { color: #004faf; text-decoration: none }
11
 
a:visited { color: #672967; text-decoration: none }
12
 
td.postheader { font-family: sans-serif }
13
 
tr.address { font-family: sans-serif }
14
 
body { background: #ffffff; color: black; }</style>
 
7
  <title>Qt 4.2: QHash::iterator Class Reference</title>
 
8
  <link href="classic.css" rel="stylesheet" type="text/css" />
15
9
</head>
16
10
<body>
17
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
12
<tr>
19
 
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
13
<td align="left" valign="top" width="32"><a href="http://www.trolltech.com/products/qt"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></a></td>
20
14
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="modules.html"><font color="#004faf">Modules</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
21
 
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QHash::iterator Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1>
 
15
<td align="right" valign="top" width="230"><a href="http://www.trolltech.com"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></a></td></tr></table><h1 align="center">QHash::iterator Class Reference<br /><sup><sup>[<a href="qtcore.html">QtCore</a> module]</sup></sup></h1>
22
16
<p>The QHash::iterator class provides an STL-style non-const iterator for <a href="qhash.html">QHash</a> and <a href="qmultihash.html">QMultiHash</a>. <a href="#details">More...</a></p>
23
 
<pre>#include &lt;QHash&gt;</pre><ul>
 
17
<pre> #include &lt;<a href="qhash.html">QHash</a>&gt;</pre><ul>
24
18
<li><a href="qhash-iterator-members.html">List of all members, including inherited members</a></li>
25
19
</ul>
26
20
<a name="public-functions"></a>
51
45
<p><a href="qhash.html">QHash</a> features both <a href="containers.html#stl-style-iterators">STL-style iterators</a> and <a href="containers.html#java-style-iterators">Java-style iterators</a>. The STL-style iterators are more low-level and more cumbersome to use; on the other hand, they are slightly faster and, for developers who already know STL, have the advantage of familiarity.</p>
52
46
<p><a href="qhash.html">QHash</a>&lt;Key, T&gt;::iterator allows you to iterate over a <a href="qhash.html">QHash</a> (or <a href="qmultihash.html">QMultiHash</a>) and to modify the value (but not the key) associated with a particular key. If you want to iterate over a const <a href="qhash.html">QHash</a>, you should use <a href="qhash-const-iterator.html">QHash::const_iterator</a>. It is generally good practice to use <a href="qhash-const-iterator.html">QHash::const_iterator</a> on a non-const <a href="qhash.html">QHash</a> as well, unless you need to change the <a href="qhash.html">QHash</a> through the iterator. Const iterators are slightly faster, and can improve code readability.</p>
53
47
<p>The default QHash::iterator constructor creates an uninitialized iterator. You must initialize it using a <a href="qhash.html">QHash</a> function like <a href="qhash.html#begin">QHash::begin</a>(), <a href="qhash.html#end">QHash::end</a>(), or <a href="qhash.html#find">QHash::find</a>() before you can start iterating. Here's a typical loop that prints all the (key, value) pairs stored in a hash:</p>
54
 
<pre>&nbsp;   QHash&lt;QString, int&gt; hash;
55
 
    hash.insert(&quot;January&quot;, 1);
56
 
    hash.insert(&quot;February&quot;, 2);
57
 
    ...
58
 
    hash.insert(&quot;December&quot;, 12);
 
48
<pre> QHash&lt;QString, int&gt; hash;
 
49
 hash.insert(&quot;January&quot;, 1);
 
50
 hash.insert(&quot;February&quot;, 2);
 
51
 ...
 
52
 hash.insert(&quot;December&quot;, 12);
59
53
 
60
 
    QHash&lt;QString, int&gt;::iterator i;
61
 
    for (i = hash.begin(); i != hash.end(); ++i)
62
 
        cout &lt;&lt; i.key() &lt;&lt; &quot;: &quot; &lt;&lt; i.value() &lt;&lt; endl;</pre>
 
54
 QHash&lt;QString, int&gt;::iterator i;
 
55
 for (i = hash.begin(); i != hash.end(); ++i)
 
56
     cout &lt;&lt; i.key() &lt;&lt; &quot;: &quot; &lt;&lt; i.value() &lt;&lt; endl;</pre>
63
57
<p>Unlike <a href="qmap.html">QMap</a>, which orders its items by key, <a href="qhash.html">QHash</a> stores its items in an arbitrary order. The only guarantee is that items that share the same key (because they were inserted using <a href="qhash.html#insertMulti">QHash::insertMulti</a>()) will appear consecutively, from the most recently to the least recently inserted value.</p>
64
58
<p>Let's see a few examples of things we can do with a QHash::iterator that we cannot do with a <a href="qhash-const-iterator.html">QHash::const_iterator</a>. Here's an example that increments every value stored in the <a href="qhash.html">QHash</a> by 2:</p>
65
 
<pre>&nbsp;   QHash&lt;QString, int&gt;::iterator i;
66
 
    for (i = hash.begin(); i != hash.end(); ++i)
67
 
        i.value() += 2;</pre>
 
59
<pre> QHash&lt;QString, int&gt;::iterator i;
 
60
 for (i = hash.begin(); i != hash.end(); ++i)
 
61
     i.value() += 2;</pre>
68
62
<p>Here's an example that removes all the items whose key is a string that starts with an underscore character:</p>
69
 
<pre>&nbsp;   QHash&lt;QString, int&gt;::iterator i = hash.begin();
70
 
    while (i != hash.end()) {
71
 
        if (i.key().startsWith(&quot;_&quot;))
72
 
            i = hash.erase(i);
73
 
        else
74
 
            ++i;
75
 
    }</pre>
 
63
<pre> QHash&lt;QString, int&gt;::iterator i = hash.begin();
 
64
 while (i != hash.end()) {
 
65
     if (i.key().startsWith(&quot;_&quot;))
 
66
         i = hash.erase(i);
 
67
     else
 
68
         ++i;
 
69
 }</pre>
76
70
<p>The call to <a href="qhash.html#erase">QHash::erase</a>() removes the item pointed to by the iterator from the hash, and returns an iterator to the next item. Here's another way of removing an item while iterating:</p>
77
 
<pre>&nbsp;   QHash&lt;QString, int&gt;::iterator i = hash.begin();
78
 
    while (i != hash.end()) {
79
 
        QHash&lt;QString, int&gt;::iterator prev = i;
80
 
        ++i;
81
 
        if (prev.key().startsWith(&quot;_&quot;))
82
 
            hash.erase(prev);
83
 
    }</pre>
 
71
<pre> QHash&lt;QString, int&gt;::iterator i = hash.begin();
 
72
 while (i != hash.end()) {
 
73
     QHash&lt;QString, int&gt;::iterator prev = i;
 
74
     ++i;
 
75
     if (prev.key().startsWith(&quot;_&quot;))
 
76
         hash.erase(prev);
 
77
 }</pre>
84
78
<p>It might be tempting to write code like this:</p>
85
 
<pre>&nbsp;   // WRONG
86
 
    while (i != hash.end()) {
87
 
        if (i.key().startsWith(&quot;_&quot;))
88
 
            hash.erase(i);
89
 
        ++i;
90
 
    }</pre>
 
79
<pre><span class="comment"> // WRONG</span>
 
80
 while (i != hash.end()) {
 
81
     if (i.key().startsWith(&quot;_&quot;))
 
82
         hash.erase(i);
 
83
     ++i;
 
84
 }</pre>
91
85
<p>However, this will potentially crash in <tt>++i</tt>, because <tt>i</tt> is a dangling iterator after the call to <a href="qhash.html#erase">erase</a>().</p>
92
86
<p>Multiple iterators can be used on the same hash. However, be aware that any modification performed directly on the <a href="qhash.html">QHash</a> has the potential of dramatically changing the order in which the items are stored in the hash, as they might cause <a href="qhash.html">QHash</a> to rehash its internal data structure. There is one notable exception: <a href="qhash.html#erase">QHash::erase</a>(). This function can safely be called while iterating, and won't affect the order of items in the hash. If you need to keep iterators over a long period of time, we recommend that you use <a href="qmap.html">QMap</a> rather than <a href="qhash.html">QHash</a>.</p>
93
87
<p>See also <a href="qhash-const-iterator.html">QHash::const_iterator</a> and <a href="qmutablehashiterator.html">QMutableHashIterator</a>.</p>
104
98
<h3 class="fn"><a name="value"></a>T &amp; iterator::value () const</h3>
105
99
<p>Returns a modifiable reference to the current item's value.</p>
106
100
<p>You can change the value of an item by using value() on the left side of an assignment, for example:</p>
107
 
<pre>&nbsp;   if (i.key() == &quot;Hello&quot;)
108
 
        i.value() = &quot;Bonjour&quot;;</pre>
 
101
<pre> if (i.key() == &quot;Hello&quot;)
 
102
     i.value() = &quot;Bonjour&quot;;</pre>
109
103
<p>See also <a href="qhash-iterator.html#key">key</a>() and <a href="qhash-iterator.html#operator-2a">operator*</a>().</p>
110
104
<h3 class="fn"><a name="operator-not-eq"></a>bool iterator::operator!= ( const iterator &amp; <i>other</i> ) const</h3>
111
105
<p>Returns true if <i>other</i> points to a different item than this iterator; otherwise returns false.</p>
160
154
<table width="100%" cellspacing="0" border="0"><tr class="address">
161
155
<td width="30%">Copyright &copy; 2006 <a href="trolltech.html">Trolltech</a></td>
162
156
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
163
 
<td width="30%" align="right"><div align="right">Qt 4.1.4</div></td>
 
157
<td width="30%" align="right"><div align="right">Qt 4.2.0</div></td>
164
158
</tr></table></div></address></body>
165
159
</html>