mi_bidirectional_map.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. Boost.MultiIndex
  10. ******************************************************************************/
  11. #include <boost/config.hpp>
  12. //[ code_mi_to_b_path_mi_bidirectional_map
  13. #include <iostream>
  14. #include <boost/tokenizer.hpp>
  15. #include <boost/multi_index_container.hpp>
  16. #include <boost/multi_index/key_extractors.hpp>
  17. #include <boost/multi_index/ordered_index.hpp>
  18. using namespace boost;
  19. using namespace boost::multi_index;
  20. // tags for accessing both sides of a bidirectional map
  21. struct from {};
  22. struct to {};
  23. // The class template bidirectional_map wraps the specification
  24. // of a bidirectional map based on multi_index_container.
  25. template<typename FromType,typename ToType>
  26. struct bidirectional_map
  27. {
  28. typedef std::pair<FromType,ToType> value_type;
  29. typedef multi_index_container<
  30. value_type,
  31. indexed_by
  32. <
  33. ordered_unique
  34. <
  35. tag<from>, member<value_type,FromType,&value_type::first>
  36. >,
  37. ordered_unique
  38. <
  39. tag<to>, member<value_type,ToType,&value_type::second>
  40. >
  41. >
  42. > type;
  43. };
  44. // A dictionary is a bidirectional map from strings to strings
  45. typedef bidirectional_map<std::string,std::string>::type dictionary;
  46. int main()
  47. {
  48. dictionary d;
  49. // Fill up our microdictionary.
  50. // first members Spanish, second members English.
  51. d.insert(dictionary::value_type("hola","hello"));
  52. d.insert(dictionary::value_type("adios","goodbye"));
  53. d.insert(dictionary::value_type("rosa","rose"));
  54. d.insert(dictionary::value_type("mesa","table"));
  55. std::cout << "enter a word" << std::endl;
  56. std::string word;
  57. std::getline(std::cin,word);
  58. // search the queried word on the from index (Spanish)
  59. dictionary::iterator it = d.get<from>().find(word);
  60. if( it != d.end() )
  61. {
  62. // the second part of the element is the equivalent in English
  63. std::cout << word << " is said "
  64. << it->second << " in English" << std::endl;
  65. }
  66. else
  67. {
  68. // word not found in Spanish, try our luck in English
  69. dictionary::index_iterator<to>::type it2 = d.get<to>().find(word);
  70. if( it2 != d.get<to>().end() )
  71. {
  72. std::cout << word << " is said "
  73. << it2->first << " in Spanish" << std::endl;
  74. }
  75. else
  76. {
  77. std::cout << "No such word in the dictionary" << std::endl;
  78. }
  79. }
  80. return 0;
  81. }
  82. //]