• 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 
10 // Boost.Bimap Example
11 //-----------------------------------------------------------------------------
12 // This example shows how to construct a bidirectional map with
13 // multi_index_container.
14 // By a bidirectional map we mean a container of elements of
15 // std::pair<const FromType,const ToType> such that no two elements exists with
16 // the same first or second value (std::map only guarantees uniqueness of the
17 // first member).
18 // Fast lookup is provided for both keys. The program features a tiny
19 // Spanish-English dictionary with online query of words in both languages.
20 
21 #include <boost/config.hpp>
22 
23 //[ code_mi_to_b_path_bidirectional_map
24 
25 #include <iostream>
26 #include <boost/tokenizer.hpp>
27 #include <boost/bimap/bimap.hpp>
28 
29 using namespace boost::bimaps;
30 
31 // A dictionary is a bidirectional map from strings to strings
32 
33 typedef bimap<std::string,std::string> dictionary;
34 typedef dictionary::value_type translation;
35 
main()36 int main()
37 {
38     dictionary d;
39 
40     // Fill up our microdictionary.
41     // first members Spanish, second members English.
42 
43     d.insert( translation("hola" ,"hello"  ));
44     d.insert( translation("adios","goodbye"));
45     d.insert( translation("rosa" ,"rose"   ));
46     d.insert( translation("mesa" ,"table"  ));
47 
48     std::cout << "enter a word" << std::endl;
49     std::string word;
50     std::getline(std::cin,word);
51 
52     // search the queried word on the from index (Spanish)
53 
54     dictionary::left_const_iterator it = d.left.find(word);
55 
56     if( it != d.left.end() )
57     {
58         // the second part of the element is the equivalent in English
59 
60         std::cout << word << " is said "
61                   << it->second /*< `it` is an iterator of the left view, so
62                                     `it->second` refers to the right element of
63                                      the relation, the word in english >*/
64                   << " in English" << std::endl;
65     }
66     else
67     {
68         // word not found in Spanish, try our luck in English
69 
70         dictionary::right_const_iterator it2 = d.right.find(word);
71         if( it2 != d.right.end() )
72         {
73             std::cout << word << " is said "
74                       << it2->second /*< `it2` is an iterator of the right view, so
75                                          `it2->second` refers to the left element of
76                                           the relation, the word in spanish >*/
77                       << " in Spanish" << std::endl;
78         }
79         else
80         {
81             std::cout << "No such word in the dictionary" << std::endl;
82         }
83     }
84 
85     return 0;
86 }
87 //]
88