]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/bimap/example/mi_to_b_path/bidirectional_map.cpp
3 // Copyright (c) 2006-2007 Matias Capeletto
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)
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
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.
21 #include <boost/config.hpp>
23 //[ code_mi_to_b_path_bidirectional_map
26 #include <boost/tokenizer.hpp>
27 #include <boost/bimap/bimap.hpp>
29 using namespace boost::bimaps
;
31 // A dictionary is a bidirectional map from strings to strings
33 typedef bimap
<std::string
,std::string
> dictionary
;
34 typedef dictionary::value_type translation
;
40 // Fill up our microdictionary.
41 // first members Spanish, second members English.
43 d
.insert( translation("hola" ,"hello" ));
44 d
.insert( translation("adios","goodbye"));
45 d
.insert( translation("rosa" ,"rose" ));
46 d
.insert( translation("mesa" ,"table" ));
48 std::cout
<< "enter a word" << std::endl
;
50 std::getline(std::cin
,word
);
52 // search the queried word on the from index (Spanish)
54 dictionary::left_const_iterator it
= d
.left
.find(word
);
56 if( it
!= d
.left
.end() )
58 // the second part of the element is the equivalent in English
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
;
68 // word not found in Spanish, try our luck in English
70 dictionary::right_const_iterator it2
= d
.right
.find(word
);
71 if( it2
!= d
.right
.end() )
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
;
81 std::cout
<< "No such word in the dictionary" << std::endl
;