]>
Commit | Line | Data |
---|---|---|
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 | //[ code_mi_to_b_path_tagged_bidirectional_map | |
22 | ||
23 | #include <iostream> | |
24 | ||
25 | #include <boost/bimap/bimap.hpp> | |
26 | ||
27 | using namespace boost::bimaps; | |
28 | ||
29 | // tags | |
30 | ||
31 | struct spanish {}; | |
32 | struct english {}; | |
33 | ||
34 | // A dictionary is a bidirectional map from strings to strings | |
35 | ||
36 | typedef bimap | |
37 | < | |
38 | tagged< std::string,spanish >, tagged< std::string,english > | |
39 | ||
40 | > dictionary; | |
41 | ||
42 | typedef dictionary::value_type translation; | |
43 | ||
44 | int main() | |
45 | { | |
46 | dictionary d; | |
47 | ||
48 | // Fill up our microdictionary. | |
49 | // first members Spanish, second members English. | |
50 | ||
51 | d.insert( translation("hola" ,"hello" )); | |
52 | d.insert( translation("adios","goodbye")); | |
53 | d.insert( translation("rosa" ,"rose" )); | |
54 | d.insert( translation("mesa" ,"table" )); | |
55 | ||
56 | std::cout << "enter a word" << std::endl; | |
57 | std::string word; | |
58 | std::getline(std::cin,word); | |
59 | ||
60 | // search the queried word on the from index (Spanish) */ | |
61 | ||
62 | dictionary::map_by<spanish>::const_iterator it = | |
63 | d.by<spanish>().find(word); | |
64 | ||
65 | if( it != d.by<spanish>().end() ) | |
66 | { | |
67 | std::cout << word << " is said " | |
68 | << it->get<english>() << " in English" << std::endl; | |
69 | } | |
70 | else | |
71 | { | |
72 | // word not found in Spanish, try our luck in English | |
73 | ||
74 | dictionary::map_by<english>::const_iterator it2 = | |
75 | d.by<english>().find(word); | |
76 | ||
77 | if( it2 != d.by<english>().end() ) | |
78 | { | |
79 | std::cout << word << " is said " | |
80 | << it2->get<spanish>() << " in Spanish" << std::endl; | |
81 | } | |
82 | else | |
83 | { | |
84 | std::cout << "No such word in the dictionary" << std::endl; | |
85 | } | |
86 | } | |
87 | ||
88 | return 0; | |
89 | } | |
90 | //] |