// Boost.Bimap // // Copyright (c) 2006-2007 Matias Capeletto // // Distributed under the Boost Software License, Version 1.0. // (See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt) /****************************************************************************** Boost.MultiIndex ******************************************************************************/ #include //[ code_mi_to_b_path_mi_bidirectional_map #include #include #include #include #include using namespace boost; using namespace boost::multi_index; // tags for accessing both sides of a bidirectional map struct from {}; struct to {}; // The class template bidirectional_map wraps the specification // of a bidirectional map based on multi_index_container. template struct bidirectional_map { typedef std::pair value_type; typedef multi_index_container< value_type, indexed_by < ordered_unique < tag, member >, ordered_unique < tag, member > > > type; }; // A dictionary is a bidirectional map from strings to strings typedef bidirectional_map::type dictionary; int main() { dictionary d; // Fill up our microdictionary. // first members Spanish, second members English. d.insert(dictionary::value_type("hola","hello")); d.insert(dictionary::value_type("adios","goodbye")); d.insert(dictionary::value_type("rosa","rose")); d.insert(dictionary::value_type("mesa","table")); std::cout << "enter a word" << std::endl; std::string word; std::getline(std::cin,word); // search the queried word on the from index (Spanish) dictionary::iterator it = d.get().find(word); if( it != d.end() ) { // the second part of the element is the equivalent in English std::cout << word << " is said " << it->second << " in English" << std::endl; } else { // word not found in Spanish, try our luck in English dictionary::index_iterator::type it2 = d.get().find(word); if( it2 != d.get().end() ) { std::cout << word << " is said " << it2->first << " in Spanish" << std::endl; } else { std::cout << "No such word in the dictionary" << std::endl; } } return 0; } //]