<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <!-- qhash.cpp --> <head> <title>Qt 4.6: iterator Class Reference</title> <link href="classic.css" rel="stylesheet" type="text/css" /> </head> <body> <a name="//apple_ref/cpp/cl/QHash/iterator"></a> <table border="0" cellpadding="0" cellspacing="0" width="100%"> <tr> <td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td> <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="functions.html"><font color="#004faf">All Functions</font></a> · <a href="overviews.html"><font color="#004faf">Overviews</font></a></td></tr></table><h1 class="title">iterator Class Reference<br /><span class="small-subtitle">(QHash::iterator)<br />[<a href="qtcore.html">QtCore</a> module]</span> </h1> <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> <pre> #include <<a href="qhash.html">QHash</a>></pre><ul> <li><a href="qhash-iterator-members.html">List of all members, including inherited members</a></li> </ul> <hr /> <a name="public-functions"></a> <h2>Public Functions</h2> <table class="alignedsummary" border="0" cellpadding="0" cellspacing="0" width="100%"> <tr><td class="memItemLeft" align="right" valign="top"></td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#iterator">iterator</a></b> ()</td></tr> <tr><td class="memItemLeft" align="right" valign="top">const Key & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#key">key</a></b> () const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">T & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#value">value</a></b> () const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-not-eq">operator!=</a></b> ( const iterator & <i>other</i> ) const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-not-eq-2">operator!=</a></b> ( const const_iterator & <i>other</i> ) const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">T & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-2a">operator*</a></b> () const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-2b">operator+</a></b> ( int <i>j</i> ) const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-2b-2b">operator++</a></b> ()</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-2b-2b-2">operator++</a></b> ( int )</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-2b-eq">operator+=</a></b> ( int <i>j</i> )</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-">operator-</a></b> ( int <i>j</i> ) const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator--">operator--</a></b> ()</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator---2">operator--</a></b> ( int )</td></tr> <tr><td class="memItemLeft" align="right" valign="top">iterator & </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator--eq">operator-=</a></b> ( int <i>j</i> )</td></tr> <tr><td class="memItemLeft" align="right" valign="top">T * </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator--gt">operator-></a></b> () const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-eq-eq">operator==</a></b> ( const iterator & <i>other</i> ) const</td></tr> <tr><td class="memItemLeft" align="right" valign="top">bool </td><td class="memItemRight" valign="bottom"><b><a href="qhash-iterator.html#operator-eq-eq-2">operator==</a></b> ( const const_iterator & <i>other</i> ) const</td></tr> </table> <a name="details"></a> <hr /> <h2>Detailed Description</h2> <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>.</p> <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> <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> <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> <pre> QHash<QString, int> hash; hash.insert("January", 1); hash.insert("February", 2); ... hash.insert("December", 12); QHash<QString, int>::iterator i; for (i = hash.begin(); i != hash.end(); ++i) cout << i.key() << ": " << i.value() << endl;</pre> <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> <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> <pre> QHash<QString, int>::iterator i; for (i = hash.begin(); i != hash.end(); ++i) i.value() += 2;</pre> <p>Here's an example that removes all the items whose key is a string that starts with an underscore character:</p> <pre> QHash<QString, int>::iterator i = hash.begin(); while (i != hash.end()) { if (i.key().startsWith("_")) i = hash.erase(i); else ++i; }</pre> <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> <pre> QHash<QString, int>::iterator i = hash.begin(); while (i != hash.end()) { QHash<QString, int>::iterator prev = i; ++i; if (prev.key().startsWith("_")) hash.erase(prev); }</pre> <p>It might be tempting to write code like this:</p> <pre><span class="comment"> // WRONG</span> while (i != hash.end()) { if (i.key().startsWith("_")) hash.erase(i); ++i; }</pre> <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> <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> <p>See also <a href="qhash-const-iterator.html">QHash::const_iterator</a> and <a href="qmutablehashiterator.html">QMutableHashIterator</a>.</p> <hr /> <h2>Member Function Documentation</h2> <a name="//apple_ref/cpp/instm/QHash::iterator/iterator"></a> <h3 class="fn"><a name="iterator"></a>iterator::iterator ()</h3> <p>Constructs an uninitialized iterator.</p> <p>Functions like <a href="qhash-iterator.html#key">key</a>(), <a href="qhash-iterator.html#value">value</a>(), and operator++() must not be called on an uninitialized iterator. Use operator=() to assign a value to it before using it.</p> <p>See also <a href="qhash.html#begin">QHash::begin</a>() and <a href="qhash.html#end">QHash::end</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/key"></a> <h3 class="fn"><a name="key"></a>const Key & iterator::key () const</h3> <p>Returns the current item's key as a const reference.</p> <p>There is no direct way of changing an item's key through an iterator, although it can be done by calling <a href="qhash.html#erase">QHash::erase</a>() followed by <a href="qhash.html#insert">QHash::insert</a>() or <a href="qhash.html#insertMulti">QHash::insertMulti</a>().</p> <p>See also <a href="qhash-iterator.html#value">value</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/value"></a> <h3 class="fn"><a name="value"></a>T & iterator::value () const</h3> <p>Returns a modifiable reference to the current item's value.</p> <p>You can change the value of an item by using value() on the left side of an assignment, for example:</p> <pre> if (i.key() == "Hello") i.value() = "Bonjour";</pre> <p>See also <a href="qhash-iterator.html#key">key</a>() and <a href="qhash-iterator.html#operator-2a">operator*</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator!="></a> <h3 class="fn"><a name="operator-not-eq"></a>bool iterator::operator!= ( const iterator & <i>other</i> ) const</h3> <p>Returns true if <i>other</i> points to a different item than this iterator; otherwise returns false.</p> <p>See also <a href="qhash-iterator.html#operator-eq-eq">operator==</a>().</p> <h3 class="fn"><a name="operator-not-eq-2"></a>bool iterator::operator!= ( const <a href="qhash-const-iterator.html">const_iterator</a> & <i>other</i> ) const</h3> <p>Returns true if <i>other</i> points to a different item than this iterator; otherwise returns false.</p> <p>See also <a href="qhash-iterator.html#operator-eq-eq">operator==</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator*"></a> <h3 class="fn"><a name="operator-2a"></a>T & iterator::operator* () const</h3> <p>Returns a modifiable reference to the current item's value.</p> <p>Same as <a href="qhash-iterator.html#value">value</a>().</p> <p>See also <a href="qhash-iterator.html#key">key</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator+"></a> <h3 class="fn"><a name="operator-2b"></a>iterator iterator::operator+ ( int <i>j</i> ) const</h3> <p>Returns an iterator to the item at <i>j</i> positions forward from this iterator. (If <i>j</i> is negative, the iterator goes backward.)</p> <p>This operation can be slow for large <i>j</i> values.</p> <p>See also <a href="qhash-iterator.html#operator-">operator-</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator++"></a> <h3 class="fn"><a name="operator-2b-2b"></a>iterator & iterator::operator++ ()</h3> <p>The prefix ++ operator (<tt>++i</tt>) advances the iterator to the next item in the hash and returns an iterator to the new current item.</p> <p>Calling this function on <a href="qhash.html#end">QHash::end</a>() leads to undefined results.</p> <p>See also <a href="qhash-iterator.html#operator--">operator--</a>().</p> <h3 class="fn"><a name="operator-2b-2b-2"></a>iterator iterator::operator++ ( int )</h3> <p>This is an overloaded function.</p> <p>The postfix ++ operator (<tt>i++</tt>) advances the iterator to the next item in the hash and returns an iterator to the previously current item.</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator+="></a> <h3 class="fn"><a name="operator-2b-eq"></a>iterator & iterator::operator+= ( int <i>j</i> )</h3> <p>Advances the iterator by <i>j</i> items. (If <i>j</i> is negative, the iterator goes backward.)</p> <p>See also <a href="qhash-iterator.html#operator--eq">operator-=</a>() and <a href="qhash-iterator.html#operator-2b">operator+</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator-"></a> <h3 class="fn"><a name="operator-"></a>iterator iterator::operator- ( int <i>j</i> ) const</h3> <p>Returns an iterator to the item at <i>j</i> positions backward from this iterator. (If <i>j</i> is negative, the iterator goes forward.)</p> <p>This operation can be slow for large <i>j</i> values.</p> <p>See also <a href="qhash-iterator.html#operator-2b">operator+</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator--"></a> <h3 class="fn"><a name="operator--"></a>iterator & iterator::operator-- ()</h3> <p>The prefix -- operator (<tt>--i</tt>) makes the preceding item current and returns an iterator pointing to the new current item.</p> <p>Calling this function on <a href="qhash.html#begin">QHash::begin</a>() leads to undefined results.</p> <p>See also <a href="qhash-iterator.html#operator-2b-2b">operator++</a>().</p> <h3 class="fn"><a name="operator---2"></a>iterator iterator::operator-- ( int )</h3> <p>This is an overloaded function.</p> <p>The postfix -- operator (<tt>i--</tt>) makes the preceding item current and returns an iterator pointing to the previously current item.</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator-="></a> <h3 class="fn"><a name="operator--eq"></a>iterator & iterator::operator-= ( int <i>j</i> )</h3> <p>Makes the iterator go back by <i>j</i> items. (If <i>j</i> is negative, the iterator goes forward.)</p> <p>See also <a href="qhash-iterator.html#operator-2b-eq">operator+=</a>() and <a href="qhash-iterator.html#operator-">operator-</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator->"></a> <h3 class="fn"><a name="operator--gt"></a>T * iterator::operator-> () const</h3> <p>Returns a pointer to the current item's value.</p> <p>See also <a href="qhash-iterator.html#value">value</a>().</p> <a name="//apple_ref/cpp/instm/QHash::iterator/operator=="></a> <h3 class="fn"><a name="operator-eq-eq"></a>bool iterator::operator== ( const iterator & <i>other</i> ) const</h3> <p>Returns true if <i>other</i> points to the same item as this iterator; otherwise returns false.</p> <p>See also <a href="qhash-iterator.html#operator-not-eq">operator!=</a>().</p> <h3 class="fn"><a name="operator-eq-eq-2"></a>bool iterator::operator== ( const <a href="qhash-const-iterator.html">const_iterator</a> & <i>other</i> ) const</h3> <p>Returns true if <i>other</i> points to the same item as this iterator; otherwise returns false.</p> <p>See also <a href="qhash-iterator.html#operator-not-eq">operator!=</a>().</p> <p /><address><hr /><div align="center"> <table width="100%" cellspacing="0" border="0"><tr class="address"> <td width="40%" align="left">Copyright © 2010 Nokia Corporation and/or its subsidiary(-ies)</td> <td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td> <td width="40%" align="right"><div align="right">Qt 4.6.3</div></td> </tr></table></div></address></body> </html>