Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / bimap / example / mighty_bimap.cpp
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 // This is the translator example from the tutorial.
21 // In this example the set type of relation is changed to allow the iteration
22 // of the container.
23
24 #include <boost/config.hpp>
25
26 //[ code_mighty_bimap
27
28 #include <iostream>
29 #include <string>
30 #include <boost/bimap/bimap.hpp>
31 #include <boost/bimap/list_of.hpp>
32 #include <boost/bimap/unordered_set_of.hpp>
33
34 struct english {};
35 struct spanish {};
36
37 int main()
38 {
39     using namespace boost::bimaps;
40
41     typedef bimap
42     <
43         unordered_set_of< tagged< std::string, spanish > >,
44         unordered_set_of< tagged< std::string, english > >,
45         list_of_relation
46
47     > translator;
48
49     translator trans;
50
51     // We have to use `push_back` because the collection of relations is
52     // a `list_of_relation`
53
54     trans.push_back( translator::value_type("hola"  ,"hello"   ) );
55     trans.push_back( translator::value_type("adios" ,"goodbye" ) );
56     trans.push_back( translator::value_type("rosa"  ,"rose"    ) );
57     trans.push_back( translator::value_type("mesa"  ,"table"   ) );
58
59     std::cout << "enter a word" << std::endl;
60     std::string word;
61     std::getline(std::cin,word);
62
63     // Search the queried word on the from index (Spanish)
64
65     translator::map_by<spanish>::const_iterator is
66         = trans.by<spanish>().find(word);
67
68     if( is != trans.by<spanish>().end() )
69     {
70         std::cout << word << " is said "
71                   << is->get<english>()
72                   << " in English" << std::endl;
73     }
74     else
75     {
76         // Word not found in Spanish, try our luck in English
77
78         translator::map_by<english>::const_iterator ie
79             = trans.by<english>().find(word);
80
81         if( ie != trans.by<english>().end() )
82         {
83             std::cout << word << " is said "
84                       << ie->get<spanish>()
85                       << " in Spanish" << std::endl;
86         }
87         else
88         {
89             // Word not found, show the possible translations
90
91             std::cout << "No such word in the dictionary"      << std::endl;
92             std::cout << "These are the possible translations" << std::endl;
93
94             for( translator::const_iterator
95                     i = trans.begin(),
96                     i_end = trans.end();
97
98                     i != i_end ; ++i )
99             {
100                 std::cout << i->get<spanish>()
101                           << " <---> "
102                           << i->get<english>()
103                           << std::endl;
104             }
105         }
106     }
107     return 0;
108 }
109 //]