mighty_bimap.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. // VC++ 8.0 warns on usage of certain Standard Library and API functions that
  9. // can be cause buffer overruns or other possible security issues if misused.
  10. // See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
  11. // But the wording of the warning is misleading and unsettling, there are no
  12. // portable alternative functions, and VC++ 8.0's own libraries use the
  13. // functions in question. So turn off the warnings.
  14. #define _CRT_SECURE_NO_DEPRECATE
  15. #define _SCL_SECURE_NO_DEPRECATE
  16. // Boost.Bimap Example
  17. //-----------------------------------------------------------------------------
  18. // This is the translator example from the tutorial.
  19. // In this example the set type of relation is changed to allow the iteration
  20. // of the container.
  21. #include <boost/config.hpp>
  22. //[ code_mighty_bimap
  23. #include <iostream>
  24. #include <string>
  25. #include <boost/bimap/bimap.hpp>
  26. #include <boost/bimap/list_of.hpp>
  27. #include <boost/bimap/unordered_set_of.hpp>
  28. struct english {};
  29. struct spanish {};
  30. int main()
  31. {
  32. using namespace boost::bimaps;
  33. typedef bimap
  34. <
  35. unordered_set_of< tagged< std::string, spanish > >,
  36. unordered_set_of< tagged< std::string, english > >,
  37. list_of_relation
  38. > translator;
  39. translator trans;
  40. // We have to use `push_back` because the collection of relations is
  41. // a `list_of_relation`
  42. trans.push_back( translator::value_type("hola" ,"hello" ) );
  43. trans.push_back( translator::value_type("adios" ,"goodbye" ) );
  44. trans.push_back( translator::value_type("rosa" ,"rose" ) );
  45. trans.push_back( translator::value_type("mesa" ,"table" ) );
  46. std::cout << "enter a word" << std::endl;
  47. std::string word;
  48. std::getline(std::cin,word);
  49. // Search the queried word on the from index (Spanish)
  50. translator::map_by<spanish>::const_iterator is
  51. = trans.by<spanish>().find(word);
  52. if( is != trans.by<spanish>().end() )
  53. {
  54. std::cout << word << " is said "
  55. << is->get<english>()
  56. << " in English" << std::endl;
  57. }
  58. else
  59. {
  60. // Word not found in Spanish, try our luck in English
  61. translator::map_by<english>::const_iterator ie
  62. = trans.by<english>().find(word);
  63. if( ie != trans.by<english>().end() )
  64. {
  65. std::cout << word << " is said "
  66. << ie->get<spanish>()
  67. << " in Spanish" << std::endl;
  68. }
  69. else
  70. {
  71. // Word not found, show the possible translations
  72. std::cout << "No such word in the dictionary" << std::endl;
  73. std::cout << "These are the possible translations" << std::endl;
  74. for( translator::const_iterator
  75. i = trans.begin(),
  76. i_end = trans.end();
  77. i != i_end ; ++i )
  78. {
  79. std::cout << i->get<spanish>()
  80. << " <---> "
  81. << i->get<english>()
  82. << std::endl;
  83. }
  84. }
  85. }
  86. return 0;
  87. }
  88. //]