• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Boost.Bimap
2 //
3 // Copyright (c) 2006-2007 Matias Capeletto
4 //
5 // Distributed under the Boost Software License, Version 1.0.
6 // (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
8 
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17 
18 // Boost.Bimap Example
19 //-----------------------------------------------------------------------------
20 
21 #include <boost/config.hpp>
22 
23 #include <string>
24 #include <iostream>
25 
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/multiset_of.hpp>
28 
29 using namespace boost::bimaps;
30 
31 
tutorial_about_info_hook()32 void tutorial_about_info_hook()
33 {
34     //[ code_tutorial_info_hook_first
35 
36     typedef bimap<
37 
38         multiset_of< std::string >, // author
39              set_of< std::string >, // title
40 
41           with_info< std::string >  // abstract
42 
43     > bm_type;
44     typedef bm_type::value_type book;
45 
46     bm_type bm;
47 
48     bm.insert(
49 
50         book( "Bjarne Stroustrup"   , "The C++ Programming Language",
51 
52               "For C++ old-timers, the first edition of this book is"
53               "the one that started it all—the font of our knowledge." )
54     );
55 
56 
57     // Print the author of the bible
58     std::cout << bm.right.at("The C++ Programming Language");
59 
60     // Print the abstract of this book
61     bm_type::left_iterator i = bm.left.find("Bjarne Stroustrup");
62     std::cout << i->info;
63     //]
64 
65     // Contrary to the two key types, the information will be mutable
66     // using iterators.
67 
68     //[ code_tutorial_info_hook_mutable
69 
70     i->info += "More details about this book";
71     //]
72 
73     // A new function is included in unique map views: info_at(key), that
74     // mimics the standard at(key) function but returned the associated
75     // information instead of the data.
76 
77     //[ code_tutorial_info_hook_info_at
78 
79     // Print the new abstract
80     std::cout << bm.right.info_at("The C++ Programming Language");
81     //]
82 }
83 
84 struct author {};
85 struct title {};
86 struct abstract {};
87 
tutorial_about_tagged_info_hook()88 void tutorial_about_tagged_info_hook()
89 {
90     //[ code_tutorial_info_hook_tagged_info
91 
92     typedef bimap<
93 
94         multiset_of< tagged< std::string, author   > >,
95              set_of< tagged< std::string, title    > >,
96 
97           with_info< tagged< std::string, abstract > >
98 
99     > bm_type;
100     typedef bm_type::value_type book;
101 
102     bm_type bm;
103 
104     bm.insert(
105 
106         book( "Bjarne Stroustrup"   , "The C++ Programming Language",
107 
108               "For C++ old-timers, the first edition of this book is"
109               "the one that started it all—the font of our knowledge." )
110     );
111 
112     // Print the author of the bible
113     std::cout << bm.by<title>().at("The C++ Programming Language");
114 
115     // Print the abstract of this book
116     bm_type::map_by<author>::iterator i = bm.by<author>().find("Bjarne Stroustrup");
117     std::cout << i->get<abstract>();
118 
119     // Contrary to the two key types, the information will be mutable
120     // using iterators.
121 
122     i->get<abstract>() += "More details about this book";
123 
124     // Print the new abstract
125     std::cout << bm.by<title>().info_at("The C++ Programming Language");
126     //]
127 }
128 
129 
bimap_without_an_info_hook()130 void bimap_without_an_info_hook()
131 {
132     //[ code_tutorial_info_hook_nothing
133 
134     typedef bimap<
135 
136         multiset_of< std::string >, // author
137              set_of< std::string >  // title
138 
139     > bm_type;
140     typedef bm_type::value_type book;
141 
142     bm_type bm;
143 
144     bm.insert( book( "Bjarne Stroustrup"   , "The C++ Programming Language" ) );
145     bm.insert( book( "Scott Meyers"        , "Effective C++"                ) );
146     bm.insert( book( "Andrei Alexandrescu" , "Modern C++ Design"            ) );
147 
148     // Print the author of Modern C++
149     std::cout << bm.right.at( "Modern C++ Design" );
150     //]
151 }
152 
153 
main()154 int main()
155 {
156     tutorial_about_info_hook();
157     tutorial_about_tagged_info_hook();
158     bimap_without_an_info_hook();
159 
160     return 0;
161 }
162 
163 
164