1<html> 2<head> 3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 4<title>Additional information</title> 5<link rel="stylesheet" href="../../boostbook.css" type="text/css"> 6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1"> 7<link rel="home" href="../../index.html" title="Chapter 1. Boost.Bimap"> 8<link rel="up" href="../the_tutorial.html" title="The tutorial"> 9<link rel="prev" href="unconstrained_sets.html" title="Unconstrained Sets"> 10<link rel="next" href="complete_instantiation_scheme.html" title="Complete instantiation scheme"> 11</head> 12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"> 13<table cellpadding="2" width="100%"><tr> 14<td valign="top"><img alt="Boost C++ Libraries" width="277" height="86" src="../../../../../../boost.png"></td> 15<td align="center"><a href="../../../../../../index.html">Home</a></td> 16<td align="center"><a href="../../../../../../libs/libraries.htm">Libraries</a></td> 17<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td> 18<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td> 19<td align="center"><a href="../../../../../../more/index.htm">More</a></td> 20</tr></table> 21<hr> 22<div class="spirit-nav"> 23<a accesskey="p" href="unconstrained_sets.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../the_tutorial.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="complete_instantiation_scheme.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 24</div> 25<div class="section"> 26<div class="titlepage"><div><div><h3 class="title"> 27<a name="boost_bimap.the_tutorial.additional_information"></a><a class="link" href="additional_information.html" title="Additional information">Additional 28 information</a> 29</h3></div></div></div> 30<p> 31 Bidirectional maps may have associated information about each relation. Suppose 32 we want to represent a books and author bidirectional map. 33 </p> 34<p> 35</p> 36<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special"><</span> 37 38 <span class="identifier">multiset_of</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="comment">// author</span> 39 <span class="identifier">set_of</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="comment">// title</span> 40 41<span class="special">></span> <span class="identifier">bm_type</span><span class="special">;</span> 42<span class="keyword">typedef</span> <span class="identifier">bm_type</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">book</span><span class="special">;</span> 43 44<span class="identifier">bm_type</span> <span class="identifier">bm</span><span class="special">;</span> 45 46<span class="identifier">bm</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">book</span><span class="special">(</span> <span class="string">"Bjarne Stroustrup"</span> <span class="special">,</span> <span class="string">"The C++ Programming Language"</span> <span class="special">)</span> <span class="special">);</span> 47<span class="identifier">bm</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">book</span><span class="special">(</span> <span class="string">"Scott Meyers"</span> <span class="special">,</span> <span class="string">"Effective C++"</span> <span class="special">)</span> <span class="special">);</span> 48<span class="identifier">bm</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> <span class="identifier">book</span><span class="special">(</span> <span class="string">"Andrei Alexandrescu"</span> <span class="special">,</span> <span class="string">"Modern C++ Design"</span> <span class="special">)</span> <span class="special">);</span> 49 50<span class="comment">// Print the author of Modern C++</span> 51<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span> <span class="string">"Modern C++ Design"</span> <span class="special">);</span> 52</pre> 53<p> 54 </p> 55<p> 56 Suppose now that we want to store abstract of each book. We have two options: 57 </p> 58<div class="orderedlist"><ol class="orderedlist" type="1"> 59<li class="listitem"> 60 Books name are unique identifiers, so we can create a separate <code class="computeroutput"><span class="identifier">std</span><span class="special">::</span><span class="identifier">map</span><span class="special"><</span> <span class="identifier">string</span><span class="special">,</span> <span class="identifier">string</span> <span class="special">></span></code> 61 that relates books names with abstracts. 62 </li> 63<li class="listitem"> 64 We can use <a href="http://www.boost.org/libs/multi_index/doc/index.html" target="_top"><span class="bold"><strong>Boost.MultiIndex</strong></span></a> for the new beast. 65 </li> 66</ol></div> 67<p> 68 Option 1 is the wrong approach, if we go this path we lost what bimap has 69 won us. We now have to maintain the logic of two interdependent containers, 70 there is an extra string stored for each book name, and the performance will 71 be worse. This is far away from being a good solution. 72 </p> 73<p> 74 Option 2 is correct. We start thinking books as entries in a table. So it 75 makes sense to start using Boost.MultiIndex. We can then add the year of 76 publication, the price, etc... and we can index this new items too. So Boost.MultiIndex 77 is a sound solution for our problem. 78 </p> 79<p> 80 The thing is that there are cases where we want to maintain bimap semantics 81 (use <code class="computeroutput"><span class="identifier">at</span><span class="special">()</span></code> 82 to find an author given a book name and the other way around) and add information 83 about the relations that we are sure we will not want to index later (like 84 the abstracts). Option 1 is not possible, option 2 neither. 85 </p> 86<p> 87 Boost.Bimap provides support for this kind of situations by means of an embedded 88 information member. You can pass an extra parameter to a bimap: <code class="computeroutput"><span class="identifier">with_info</span><span class="special"><</span> 89 <span class="identifier">InfoType</span> <span class="special">></span></code> 90 and an <code class="computeroutput"><span class="identifier">info</span></code> member of type 91 <code class="computeroutput"><span class="identifier">InfoType</span></code> will appear in the 92 relation and bimap pairs. 93 </p> 94<p> 95 <span class="inlinemediaobject"><img src="../../images/bimap/relation.and.pair.with.info.png" alt="relation.and.pair.with.info"></span> 96 </p> 97<p> 98 Relations and bimap pairs constructors will take an extra argument. If only 99 two arguments are used, the information will be initialized with their default 100 constructor. 101 </p> 102<p> 103</p> 104<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special"><</span> 105 106 <span class="identifier">multiset_of</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="comment">// author</span> 107 <span class="identifier">set_of</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="comment">// title</span> 108 109 <span class="identifier">with_info</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="comment">// abstract</span> 110 111<span class="special">></span> <span class="identifier">bm_type</span><span class="special">;</span> 112<span class="keyword">typedef</span> <span class="identifier">bm_type</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">book</span><span class="special">;</span> 113 114<span class="identifier">bm_type</span> <span class="identifier">bm</span><span class="special">;</span> 115 116<span class="identifier">bm</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> 117 118 <span class="identifier">book</span><span class="special">(</span> <span class="string">"Bjarne Stroustrup"</span> <span class="special">,</span> <span class="string">"The C++ Programming Language"</span><span class="special">,</span> 119 120 <span class="string">"For C++ old-timers, the first edition of this book is"</span> 121 <span class="string">"the one that started it all—the font of our knowledge."</span> <span class="special">)</span> 122<span class="special">);</span> 123 124 125<span class="comment">// Print the author of the bible</span> 126<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">at</span><span class="special">(</span><span class="string">"The C++ Programming Language"</span><span class="special">);</span> 127 128<span class="comment">// Print the abstract of this book</span> 129<span class="identifier">bm_type</span><span class="special">::</span><span class="identifier">left_iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">left</span><span class="special">.</span><span class="identifier">find</span><span class="special">(</span><span class="string">"Bjarne Stroustrup"</span><span class="special">);</span> 130<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">info</span><span class="special">;</span> 131</pre> 132<p> 133 </p> 134<p> 135 Contrary to the two key types, the information will be mutable using iterators. 136 </p> 137<p> 138</p> 139<pre class="programlisting"><span class="identifier">i</span><span class="special">-></span><span class="identifier">info</span> <span class="special">+=</span> <span class="string">"More details about this book"</span><span class="special">;</span> 140</pre> 141<p> 142 </p> 143<p> 144 A new function is included in <span class="emphasis"><em>unique</em></span> map views: <code class="computeroutput"><span class="identifier">info_at</span><span class="special">(</span><span class="identifier">key</span><span class="special">)</span></code>, that 145 mimics the standard <code class="computeroutput"><span class="identifier">at</span><span class="special">(</span><span class="identifier">key</span><span class="special">)</span></code> function 146 but returned the associated information instead of the data. 147 </p> 148<p> 149</p> 150<pre class="programlisting"><span class="comment">// Print the new abstract</span> 151<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">right</span><span class="special">.</span><span class="identifier">info_at</span><span class="special">(</span><span class="string">"The C++ Programming Language"</span><span class="special">);</span> 152</pre> 153<p> 154 </p> 155<p> 156 The info member can be tagged just as the left or the right member. The following 157 is a rewrite of the above example using user defined names: 158 </p> 159<p> 160</p> 161<pre class="programlisting"><span class="keyword">typedef</span> <span class="identifier">bimap</span><span class="special"><</span> 162 163 <span class="identifier">multiset_of</span><span class="special"><</span> <span class="identifier">tagged</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="identifier">author</span> <span class="special">></span> <span class="special">>,</span> 164 <span class="identifier">set_of</span><span class="special"><</span> <span class="identifier">tagged</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="identifier">title</span> <span class="special">></span> <span class="special">>,</span> 165 166 <span class="identifier">with_info</span><span class="special"><</span> <span class="identifier">tagged</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="identifier">abstract</span> <span class="special">></span> <span class="special">></span> 167 168<span class="special">></span> <span class="identifier">bm_type</span><span class="special">;</span> 169<span class="keyword">typedef</span> <span class="identifier">bm_type</span><span class="special">::</span><span class="identifier">value_type</span> <span class="identifier">book</span><span class="special">;</span> 170 171<span class="identifier">bm_type</span> <span class="identifier">bm</span><span class="special">;</span> 172 173<span class="identifier">bm</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span> 174 175 <span class="identifier">book</span><span class="special">(</span> <span class="string">"Bjarne Stroustrup"</span> <span class="special">,</span> <span class="string">"The C++ Programming Language"</span><span class="special">,</span> 176 177 <span class="string">"For C++ old-timers, the first edition of this book is"</span> 178 <span class="string">"the one that started it all—the font of our knowledge."</span> <span class="special">)</span> 179<span class="special">);</span> 180 181<span class="comment">// Print the author of the bible</span> 182<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">by</span><span class="special"><</span><span class="identifier">title</span><span class="special">>().</span><span class="identifier">at</span><span class="special">(</span><span class="string">"The C++ Programming Language"</span><span class="special">);</span> 183 184<span class="comment">// Print the abstract of this book</span> 185<span class="identifier">bm_type</span><span class="special">::</span><span class="identifier">map_by</span><span class="special"><</span><span class="identifier">author</span><span class="special">>::</span><span class="identifier">iterator</span> <span class="identifier">i</span> <span class="special">=</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">by</span><span class="special"><</span><span class="identifier">author</span><span class="special">>().</span><span class="identifier">find</span><span class="special">(</span><span class="string">"Bjarne Stroustrup"</span><span class="special">);</span> 186<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">get</span><span class="special"><</span><span class="identifier">abstract</span><span class="special">>();</span> 187 188<span class="comment">// Contrary to the two key types, the information will be mutable</span> 189<span class="comment">// using iterators.</span> 190 191<span class="identifier">i</span><span class="special">-></span><span class="identifier">get</span><span class="special"><</span><span class="identifier">abstract</span><span class="special">>()</span> <span class="special">+=</span> <span class="string">"More details about this book"</span><span class="special">;</span> 192 193<span class="comment">// Print the new abstract</span> 194<span class="identifier">std</span><span class="special">::</span><span class="identifier">cout</span> <span class="special"><<</span> <span class="identifier">bm</span><span class="special">.</span><span class="identifier">by</span><span class="special"><</span><span class="identifier">title</span><span class="special">>().</span><span class="identifier">info_at</span><span class="special">(</span><span class="string">"The C++ Programming Language"</span><span class="special">);</span> 195</pre> 196<p> 197 </p> 198<p> 199 <a href="../../../../example/tutorial_info_hook.cpp" target="_top">Go to source code</a> 200 </p> 201</div> 202<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr> 203<td align="left"></td> 204<td align="right"><div class="copyright-footer">Copyright © 2006-2012 Matias Capeletto<p> 205 Distributed under the Boost Software License, Version 1.0. (See accompanying 206 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>) 207 </p> 208</div></td> 209</tr></table> 210<hr> 211<div class="spirit-nav"> 212<a accesskey="p" href="unconstrained_sets.html"><img src="../../../../../../doc/src/images/prev.png" alt="Prev"></a><a accesskey="u" href="../the_tutorial.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="complete_instantiation_scheme.html"><img src="../../../../../../doc/src/images/next.png" alt="Next"></a> 213</div> 214</body> 215</html> 216