1 | <html> |
---|
2 | <head> |
---|
3 | <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> |
---|
4 | <title> Tutorial</title> |
---|
5 | <link rel="stylesheet" href="../boostbook.css" type="text/css"> |
---|
6 | <meta name="generator" content="DocBook XSL Stylesheets V1.68.1"> |
---|
7 | <link rel="start" href="../index.html" title="The Boost C++ Libraries BoostBook Documentation Subset"> |
---|
8 | <link rel="up" href="../hash.html" title="Chapter 7. Boost.Functional/Hash"> |
---|
9 | <link rel="prev" href="../hash.html" title="Chapter 7. Boost.Functional/Hash"> |
---|
10 | <link rel="next" href="custom.html" title=" Extending boost::hash for a custom data type"> |
---|
11 | </head> |
---|
12 | <body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> |
---|
13 | <table cellpadding="2" width="100%"> |
---|
14 | <td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../boost.png"></td> |
---|
15 | <td align="center"><a href="../../../index.htm">Home</a></td> |
---|
16 | <td align="center"><a href="../../../libs/libraries.htm">Libraries</a></td> |
---|
17 | <td align="center"><a href="../../../people/people.htm">People</a></td> |
---|
18 | <td align="center"><a href="../../../more/faq.htm">FAQ</a></td> |
---|
19 | <td align="center"><a href="../../../more/index.htm">More</a></td> |
---|
20 | </table> |
---|
21 | <hr> |
---|
22 | <div class="spirit-nav"> |
---|
23 | <a accesskey="p" href="../hash.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="custom.html"><img src="../images/next.png" alt="Next"></a> |
---|
24 | </div> |
---|
25 | <div class="section" lang="en"> |
---|
26 | <div class="titlepage"><div><div><h2 class="title" style="clear: both"> |
---|
27 | <a name="hash.tutorial"></a> Tutorial</h2></div></div></div> |
---|
28 | <p> |
---|
29 | When using a hash index with <a href="../../../libs/multi_index/doc/index.html" target="_top">Boost.MultiIndex</a>, |
---|
30 | you don't need to do anything to use <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> |
---|
31 | as it uses it by default. To find out how to use a user-defined type, read |
---|
32 | the <a href="custom.html" title=" Extending boost::hash for a custom data type">section on extending boost::hash for a custom |
---|
33 | data type</a>. |
---|
34 | </p> |
---|
35 | <p> |
---|
36 | If your standard library supplies its own implementation of the unordered associative |
---|
37 | containers and you wish to use <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code>, |
---|
38 | just use an extra template parameter: |
---|
39 | </p> |
---|
40 | <pre class="programlisting"> |
---|
41 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_multiset</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">></span> |
---|
42 | <span class="identifier">set_of_ints</span><span class="special">;</span> |
---|
43 | |
---|
44 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_set</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="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">>,</span> <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">pair</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="keyword">int</span><span class="special">></span> <span class="special">></span> |
---|
45 | <span class="identifier">set_of_pairs</span><span class="special">;</span> |
---|
46 | |
---|
47 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">unordered_map</span><span class="special"><</span><span class="keyword">int</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">,</span> <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">int</span><span class="special">></span> <span class="special">></span> <span class="identifier">map_int_to_string</span><span class="special">;</span> |
---|
48 | </pre> |
---|
49 | <p> |
---|
50 | To use <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code> directly, create |
---|
51 | an instance and call it as a function: |
---|
52 | </p> |
---|
53 | <pre class="programlisting"> |
---|
54 | <span class="preprocessor">#include</span> <span class="special"><</span><code class="computeroutput"><a href="reference.html#header.boost.functional.hash.hpp" title="Header <boost/functional/hash.hpp>">boost/functional/hash.hpp</a></code><span class="special">></span> |
---|
55 | |
---|
56 | <span class="keyword">int</span> <span class="identifier">main</span><span class="special">()</span> |
---|
57 | <span class="special">{</span> |
---|
58 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">string</span><span class="special">></span> <span class="identifier">string_hash</span><span class="special">;</span> |
---|
59 | |
---|
60 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span> <span class="identifier">h</span> <span class="special">=</span> <span class="identifier">string_hash</span><span class="special">(</span><span class="string">"Hash me"</span><span class="special">);</span> |
---|
61 | <span class="special">}</span> |
---|
62 | </pre> |
---|
63 | <p> |
---|
64 | For an example of generic use, here is a function to generate a vector containing |
---|
65 | the hashes of the elements of a container: |
---|
66 | </p> |
---|
67 | <pre class="programlisting"> |
---|
68 | <span class="keyword">template</span> <span class="special"><</span><span class="keyword">class</span> <span class="identifier">Container</span><span class="special">></span> |
---|
69 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">></span> <span class="identifier">get_hashes</span><span class="special">(</span><span class="identifier">Container</span> <span class="keyword">const</span><span class="special">&</span> <span class="identifier">x</span><span class="special">)</span> |
---|
70 | <span class="special">{</span> |
---|
71 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special"><</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">size_t</span><span class="special">></span> <span class="identifier">hashes</span><span class="special">;</span> |
---|
72 | <span class="identifier">std</span><span class="special">::</span><span class="identifier">transform</span><span class="special">(</span><span class="identifier">x</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span> <span class="identifier">x</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">insert_iterator</span><span class="special">(</span><span class="identifier">hashes</span><span class="special">),</span> |
---|
73 | <code class="computeroutput"><a href="../boost/hash.html" title="Struct template hash">boost::hash</a></code><span class="special"><</span><span class="keyword">typename</span> <span class="identifier">Container</span><span class="special">::</span><span class="identifier">value_type</span><span class="special">>());</span> |
---|
74 | |
---|
75 | <span class="keyword">return</span> <span class="identifier">hashes</span><span class="special">;</span> |
---|
76 | <span class="special">}</span> |
---|
77 | </pre> |
---|
78 | </div> |
---|
79 | <table width="100%"><tr> |
---|
80 | <td align="left"></td> |
---|
81 | <td align="right"><small>Copyright © 2005, 2006 Daniel James</small></td> |
---|
82 | </tr></table> |
---|
83 | <hr> |
---|
84 | <div class="spirit-nav"> |
---|
85 | <a accesskey="p" href="../hash.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../hash.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="custom.html"><img src="../images/next.png" alt="Next"></a> |
---|
86 | </div> |
---|
87 | </body> |
---|
88 | </html> |
---|