1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter 44. Boost.Unordered</title>
<link rel="stylesheet" href="../../doc/src/boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
<link rel="home" href="index.html" title="The Boost C++ Libraries BoostBook Documentation Subset">
<link rel="up" href="libraries.html" title="Part I. The Boost C++ Libraries (BoostBook Subset)">
<link rel="prev" href="boost_units/TODO.html" title="TODO">
<link rel="next" href="unordered/buckets.html" title="The Data Structure">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../boost.png"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="../../libs/libraries.htm">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_units/TODO.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unordered/buckets.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="unordered"></a>Chapter 44. Boost.Unordered</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Daniel</span> <span class="surname">James</span>
</h3></div></div>
<div><p class="copyright">Copyright © 2003, 2004 Jeremy B. Maitin-Shepard</p></div>
<div><p class="copyright">Copyright © 2005-2008 Daniel
James</p></div>
<div><div class="legalnotice">
<a name="unordered.legal"></a><p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl class="toc">
<dt><span class="section"><a href="unordered.html#unordered.intro">Introduction</a></span></dt>
<dt><span class="section"><a href="unordered/buckets.html">The Data Structure</a></span></dt>
<dt><span class="section"><a href="unordered/hash_equality.html">Equality Predicates and Hash Functions</a></span></dt>
<dt><span class="section"><a href="unordered/comparison.html">Comparison with Associative Containers</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html">Standard Compliance</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.move">Move emulation</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.allocator_compliance">Use of allocators</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.construction">Construction/Destruction
using allocators</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.pointer_traits">Pointer Traits</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.pairs0">Pairs</a></span></dt>
<dt><span class="section"><a href="unordered/compliance.html#unordered.compliance.misc">Miscellaneous</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="unordered/rationale.html">Implementation Rationale</a></span></dt>
<dt><span class="section"><a href="unordered/changes.html">Change Log</a></span></dt>
<dt><span class="section"><a href="unordered/reference.html">Reference</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="unordered/reference.html#header.boost.unordered_set_hpp">Header <boost/unordered_set.hpp></a></span></dt>
<dt><span class="section"><a href="unordered/reference.html#header.boost.unordered_map_hpp">Header <boost/unordered_map.hpp></a></span></dt>
</dl></dd>
<dt><span class="section"><a href="unordered/bibliography.html">Bibliography</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="unordered.intro"></a><a class="link" href="unordered.html#unordered.intro" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
For accessing data based on key lookup, the C++ standard library offers <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">set</span></code>,
<code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span></code>, <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">multiset</span></code>
and <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">multimap</span></code>. These are generally implemented
using balanced binary trees so that lookup time has logarithmic complexity.
That is generally okay, but in many cases a <a href="http://en.wikipedia.org/wiki/Hash_table" target="_top">hash
table</a> can perform better, as accessing data has constant complexity,
on average. The worst case complexity is linear, but that occurs rarely and
with some care, can be avoided.
</p>
<p>
Also, the existing containers require a 'less than' comparison object to order
their elements. For some data types this is impossible to implement or isn't
practical. In contrast, a hash table only needs an equality function and a
hash function for the key.
</p>
<p>
With this in mind, unordered associative containers were added to the C++ standard.
This is an implementation of the containers described in C++11, with some
<a class="link" href="unordered/compliance.html" title="Standard Compliance">deviations from the standard</a> in
order to work with non-C++11 compilers and libraries.
</p>
<p>
<code class="computeroutput"><span class="identifier">unordered_set</span></code> and <code class="computeroutput"><span class="identifier">unordered_multiset</span></code> are defined in the header
<<code class="computeroutput"><a class="link" href="unordered/reference.html#header.boost.unordered_set_hpp" title="Header <boost/unordered_set.hpp>">boost/unordered_set.hpp</a></code>>
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">template</span> <span class="special"><</span>
<span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special"><</span><span class="identifier">Key</span><span class="special">></span> <span class="special">></span>
<span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_set.html" title="Class template unordered_set">unordered_set</a></code><span class="special">;</span>
<span class="keyword">template</span><span class="special"><</span>
<span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special"><</span><span class="identifier">Key</span><span class="special">></span> <span class="special">></span>
<span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_multiset.html" title="Class template unordered_multiset">unordered_multiset</a></code><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
<code class="computeroutput"><span class="identifier">unordered_map</span></code> and <code class="computeroutput"><span class="identifier">unordered_multimap</span></code> are defined in the header
<<code class="computeroutput"><a class="link" href="unordered/reference.html#header.boost.unordered_map_hpp" title="Header <boost/unordered_map.hpp>">boost/unordered_map.hpp</a></code>>
</p>
<pre class="programlisting"><span class="keyword">namespace</span> <span class="identifier">boost</span> <span class="special">{</span>
<span class="keyword">template</span> <span class="special"><</span>
<span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Mapped</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Key</span> <span class="keyword">const</span><span class="special">,</span> <span class="identifier">Mapped</span><span class="special">></span> <span class="special">></span> <span class="special">></span>
<span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_map.html" title="Class template unordered_map">unordered_map</a></code><span class="special">;</span>
<span class="keyword">template</span><span class="special"><</span>
<span class="keyword">class</span> <span class="identifier">Key</span><span class="special">,</span> <span class="keyword">class</span> <span class="identifier">Mapped</span><span class="special">,</span>
<span class="keyword">class</span> <span class="identifier">Hash</span> <span class="special">=</span> <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Pred</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">equal_to</span><span class="special"><</span><span class="identifier">Key</span><span class="special">>,</span>
<span class="keyword">class</span> <span class="identifier">Alloc</span> <span class="special">=</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">allocator</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="identifier">Key</span> <span class="keyword">const</span><span class="special">,</span> <span class="identifier">Mapped</span><span class="special">></span> <span class="special">></span> <span class="special">></span>
<span class="keyword">class</span> <code class="computeroutput"><a class="link" href="boost/unordered_multimap.html" title="Class template unordered_multimap">unordered_multimap</a></code><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
When using Boost.TR1, these classes are included from <code class="computeroutput"><span class="special"><</span><span class="identifier">unordered_set</span><span class="special">></span></code>
and <code class="computeroutput"><span class="special"><</span><span class="identifier">unordered_map</span><span class="special">></span></code>, with the classes added to the <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">tr1</span></code>
namespace.
</p>
<p>
The containers are used in a similar manner to the normal associative containers:
</p>
<p>
</p>
<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">unordered_map</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <span class="keyword">int</span><span class="special">></span> <span class="identifier">map</span><span class="special">;</span>
<span class="identifier">map</span> <span class="identifier">x</span><span class="special">;</span>
<span class="identifier">x</span><span class="special">[</span><span class="string">"one"</span><span class="special">]</span> <span class="special">=</span> <span class="number">1</span><span class="special">;</span>
<span class="identifier">x</span><span class="special">[</span><span class="string">"two"</span><span class="special">]</span> <span class="special">=</span> <span class="number">2</span><span class="special">;</span>
<span class="identifier">x</span><span class="special">[</span><span class="string">"three"</span><span class="special">]</span> <span class="special">=</span> <span class="number">3</span><span class="special">;</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"one"</span><span class="special">)</span> <span class="special">==</span> <span class="number">1</span><span class="special">);</span>
<span class="identifier">assert</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="string">"missing"</span><span class="special">)</span> <span class="special">==</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">end</span><span class="special">());</span>
</pre>
<p>
</p>
<p>
But since the elements aren't ordered, the output of:
</p>
<p>
</p>
<pre class="programlisting"><span class="identifier">BOOST_FOREACH</span><span class="special">(</span><span class="identifier">map</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">i</span><span class="special">,</span> <span class="identifier">x</span><span class="special">)</span> <span class="special">{</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span><span class="special"><<</span><span class="identifier">i</span><span class="special">.</span><span class="identifier">first</span><span class="special"><<</span><span class="string">","</span><span class="special"><<</span><span class="identifier">i</span><span class="special">.</span><span class="identifier">second</span><span class="special"><<</span><span class="string">"\n"</span><span class="special">;</span>
<span class="special">}</span>
</pre>
<p>
</p>
<p>
can be in any order. For example, it might be:
</p>
<pre class="programlisting"><span class="identifier">two</span><span class="special">,</span><span class="number">2</span>
<span class="identifier">one</span><span class="special">,</span><span class="number">1</span>
<span class="identifier">three</span><span class="special">,</span><span class="number">3</span>
</pre>
<p>
To store an object in an unordered associative container requires both an key
equality function and a hash function. The default function objects in the
standard containers support a few basic types including integer types, floating
point types, pointer types, and the standard strings. Since Boost.Unordered
uses <code class="computeroutput"><a class="link" href="boost/hash.html" title="Struct template hash">boost::hash</a></code> it also supports
some other types, including standard containers. To use any types not supported
by these methods you have to <a class="link" href="hash/custom.html" title="Extending boost::hash for a custom data type">extend Boost.Hash
to support the type</a> or use your own custom equality predicates and hash
functions. See the <a class="link" href="unordered/hash_equality.html" title="Equality Predicates and Hash Functions">Equality Predicates
and Hash Functions</a> section for more details.
</p>
<p>
There are other differences, which are listed in the <a class="link" href="unordered/comparison.html" title="Comparison with Associative Containers">Comparison
with Associative Containers</a> section.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: September 02, 2017 at 10:04:14 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="boost_units/TODO.html"><img src="../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="libraries.html"><img src="../../doc/src/images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="../../doc/src/images/home.png" alt="Home"></a><a accesskey="n" href="unordered/buckets.html"><img src="../../doc/src/images/next.png" alt="Next"></a>
</div>
</body>
</html>
|