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 -->
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" />
17
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" 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="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="modules.html"><font color="#004faf">Modules</font></a> · <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 <QHash></pre><ul>
17
<pre> #include <<a href="qhash.html">QHash</a>></pre><ul>
24
18
<li><a href="qhash-iterator-members.html">List of all members, including inherited members</a></li>
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><Key, T>::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> QHash<QString, int> hash;
55
hash.insert("January", 1);
56
hash.insert("February", 2);
58
hash.insert("December", 12);
48
<pre> QHash<QString, int> hash;
49
hash.insert("January", 1);
50
hash.insert("February", 2);
52
hash.insert("December", 12);
60
QHash<QString, int>::iterator i;
61
for (i = hash.begin(); i != hash.end(); ++i)
62
cout << i.key() << ": " << i.value() << endl;</pre>
54
QHash<QString, int>::iterator i;
55
for (i = hash.begin(); i != hash.end(); ++i)
56
cout << i.key() << ": " << i.value() << 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> QHash<QString, int>::iterator i;
66
for (i = hash.begin(); i != hash.end(); ++i)
59
<pre> QHash<QString, int>::iterator i;
60
for (i = hash.begin(); i != hash.end(); ++i)
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> QHash<QString, int>::iterator i = hash.begin();
70
while (i != hash.end()) {
71
if (i.key().startsWith("_"))
63
<pre> QHash<QString, int>::iterator i = hash.begin();
64
while (i != hash.end()) {
65
if (i.key().startsWith("_"))
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> QHash<QString, int>::iterator i = hash.begin();
78
while (i != hash.end()) {
79
QHash<QString, int>::iterator prev = i;
81
if (prev.key().startsWith("_"))
71
<pre> QHash<QString, int>::iterator i = hash.begin();
72
while (i != hash.end()) {
73
QHash<QString, int>::iterator prev = i;
75
if (prev.key().startsWith("_"))
84
78
<p>It might be tempting to write code like this:</p>
86
while (i != hash.end()) {
87
if (i.key().startsWith("_"))
79
<pre><span class="comment"> // WRONG</span>
80
while (i != hash.end()) {
81
if (i.key().startsWith("_"))
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 & 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> if (i.key() == "Hello")
108
i.value() = "Bonjour";</pre>
101
<pre> if (i.key() == "Hello")
102
i.value() = "Bonjour";</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 & <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>