1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/src/corelib/tools/qhash.cpp -->
7
<title>Qt 4.0: QMultiHash Class Reference</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<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="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<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">QMultiHash Class Reference</h1>
21
<p>The QMultiHash class is a convenience <a href="qhash.html">QHash</a> subclass that provides multi-valued hashes. <a href="#details">More...</a></p>
22
<pre>#include <QMultiHash></pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
23
<p>Inherits <a href="qhash.html">QHash<Key, T></a>.</p>
24
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
26
<li><a href="qmultihash-members.html">List of all members, including inherited members</a></li>
28
<a name="public-functions"></a>
29
<h3>Public Functions</h3>
31
<li><div class="fn"/><b><a href="qmultihash.html#QMultiHash">QMultiHash</a></b> ()</li>
32
<li><div class="fn"/><b><a href="qmultihash.html#QMultiHash-2">QMultiHash</a></b> ( const QHash<Key, T> & <i>other</i> )</li>
33
<li><div class="fn"/>QHash<Key, T>::iterator <b><a href="qmultihash.html#insert">insert</a></b> ( const Key & <i>key</i>, const T & <i>value</i> )</li>
34
<li><div class="fn"/>QHash<Key, T>::iterator <b><a href="qmultihash.html#replace">replace</a></b> ( const Key & <i>key</i>, const T & <i>value</i> )</li>
35
<li><div class="fn"/>QMultiHash <b><a href="qmultihash.html#operator-2b">operator+</a></b> ( const QMultiHash & <i>other</i> ) const</li>
36
<li><div class="fn"/>QMultiHash & <b><a href="qmultihash.html#operator-2b-eq">operator+=</a></b> ( const QMultiHash & <i>other</i> )</li>
39
<li><div class="fn"/>36 public functions inherited from <a href="qhash.html#public-functions">QHash</a></li>
41
<a name="details"></a>
43
<h2>Detailed Description</h2>
44
<p>The QMultiHash class is a convenience <a href="qhash.html">QHash</a> subclass that provides multi-valued hashes.</p>
45
<p>QMultiHash<Key, T> is one of Qt's generic <a href="containers.html#container-classes">container classes</a>. It inherits <a href="qhash.html">QHash</a> and extends it with a few convenience functions that make it more suitable than <a href="qhash.html">QHash</a> for storing multi-valued hashes. A multi-valued hash is a hash that allows multiple values with the same key; <a href="qhash.html">QHash</a> normally doesn't allow that, unless you call <a href="qhash.html#insertMulti">QHash::insertMulti</a>().</p>
46
<p>Because QMultiHash inherits <a href="qhash.html">QHash</a>, all of <a href="qhash.html">QHash</a>'s functionality also applies to QMultiHash. For example, you can use <a href="qhash.html#isEmpty">isEmpty</a>() to test whether the hash is empty, and you can traverse a QMultiHash using <a href="qhash.html">QHash</a>'s iterator classes (for example, <a href="qhashiterator.html">QHashIterator</a>). But in addition, it provides an <a href="qmultihash.html#insert">insert</a>() function that corresponds to <a href="qhash.html#insertMulti">QHash::insertMulti</a>(), and a <a href="qmultihash.html#replace">replace</a>() function that corresponds to <a href="qhash.html#insert">QHash::insert</a>(). It also provides convenient operator+() and operator+=().</p>
48
<pre> QMultiHash<QString, int> hash1, hash2, hash3;
50
hash1.insert("plenty", 100);
51
hash1.insert("plenty", 2000);
54
hash2.insert("plenty", 5000);
57
hash3 = hash1 + hash2;
58
// hash3.size() == 3</pre>
59
<p>Unlike <a href="qhash.html">QHash</a>, QMultiHash provides no operator[]. Use <a href="qhash.html#value">value</a>() or <a href="qmultihash.html#replace">replace</a>() if you want to access the most recently inserted item with a certain key.</p>
60
<p>If you want to retrieve all the values for a single key, you can use values(const Key &key), which returns a <a href="qlist.html">QList</a><T>:</p>
61
<pre> QList<int> values = hash.values("plenty");
62
for (int i = 0; i < values.size(); ++i)
63
cout << values.at(i) << endl;</pre>
64
<p>The items that share the same key are available from most recently to least recently inserted.</p>
65
<p>A more efficient approach is to use QHashIterator::findNextKey() or QMutableHashIterator::findNextKey():</p>
66
<pre> QHashIterator<QString, int> i(hash);
67
while (i.findNextKey("plenty"))
68
cout << i.value() << endl;</pre>
69
<p>If you prefer the STL-style iterators, you can call <a href="qhash.html#find">find</a>() to get the iterator for the first item with a key and iterate from there:</p>
70
<pre> QMultiHash<QString, int>::iterator i = hash.find("plenty");
71
while (i != hash.end() && i.key() == "plenty") {
72
cout << i.value() << endl;
75
<p>QMultiHash's key and value data types must be <a href="containers.html#assignable-data-types">assignable data types</a>. You cannot, for example, store a <a href="qwidget.html">QWidget</a> as a value; instead, store a <a href="qwidget.html">QWidget</a> *. In addition, QMultiHash's key type must provide operator==(), and there must also be a global <a href="qhash.html#qHashx">qHash</a>() function that returns a hash value for an argument of the key's type. See the <a href="qhash.html">QHash</a> documentation for details.</p>
76
<p>See also <a href="qhash.html">QHash</a>, <a href="qhashiterator.html">QHashIterator</a>, <a href="qmutablehashiterator.html">QMutableHashIterator</a>, and <a href="qmultimap.html">QMultiMap</a>.</p>
78
<h2>Member Function Documentation</h2>
79
<h3 class="fn"><a name="QMultiHash"></a>QMultiHash::QMultiHash ()</h3>
80
<p>Constructs an empty hash.</p>
81
<h3 class="fn"><a name="QMultiHash-2"></a>QMultiHash::QMultiHash ( const <a href="qhash.html">QHash</a><Key, T> & <i>other</i> )</h3>
82
<p>Constructs a copy of <i>other</i> (which can be a <a href="qhash.html">QHash</a> or a <a href="qmultihash.html">QMultiHash</a>).</p>
83
<p>See also <a href="qhash.html#operator-eq">operator=</a>().</p>
84
<h3 class="fn"><a name="insert"></a><a href="qhash.html">QHash</a><Key, T>::iterator QMultiHash::insert ( const Key & <i>key</i>, const T & <i>value</i> )</h3>
85
<p>Inserts a new item with the key <i>key</i> and a value of <i>value</i>.</p>
86
<p>If there is already an item with the same key in the hash, this function will simply create a new one. (This behavior is different from <a href="qmultihash.html#replace">replace</a>(), which overwrites the value of an existing item.)</p>
87
<p>See also <a href="qmultihash.html#replace">replace</a>().</p>
88
<h3 class="fn"><a name="replace"></a><a href="qhash.html">QHash</a><Key, T>::iterator QMultiHash::replace ( const Key & <i>key</i>, const T & <i>value</i> )</h3>
89
<p>Inserts a new item with the key <i>key</i> and a value of <i>value</i>.</p>
90
<p>If there is already an item with the key <i>key</i>, that item's value is replaced with <i>value</i>.</p>
91
<p>If there are multiple items with the key <i>key</i>, the most recently inserted item's value is replaced with <i>value</i>.</p>
92
<p>See also <a href="qmultihash.html#insert">insert</a>().</p>
93
<h3 class="fn"><a name="operator-2b"></a>QMultiHash QMultiHash::operator+ ( const QMultiHash & <i>other</i> ) const</h3>
94
<p>Returns a hash that contains all the items in this hash in addition to all the items in <i>other</i>. If a key is common to both hashes, the resulting hash will contain the key multiple times.</p>
95
<p>See also <a href="qmultihash.html#operator-2b-eq">operator+=</a>().</p>
96
<h3 class="fn"><a name="operator-2b-eq"></a>QMultiHash & QMultiHash::operator+= ( const QMultiHash & <i>other</i> )</h3>
97
<p>Inserts all the items in the <i>other</i> hash into this hash and returns a reference to this hash.</p>
98
<p>See also <a href="qmultihash.html#insert">insert</a>().</p>
99
<p /><address><hr /><div align="center">
100
<table width="100%" cellspacing="0" border="0"><tr class="address">
101
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
102
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
103
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
104
</tr></table></div></address></body>