tutorial_info_hook.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #include <boost/config.hpp>
  19. #include <string>
  20. #include <iostream>
  21. #include <boost/bimap/bimap.hpp>
  22. #include <boost/bimap/multiset_of.hpp>
  23. using namespace boost::bimaps;
  24. void tutorial_about_info_hook()
  25. {
  26. //[ code_tutorial_info_hook_first
  27. typedef bimap<
  28. multiset_of< std::string >, // author
  29. set_of< std::string >, // title
  30. with_info< std::string > // abstract
  31. > bm_type;
  32. typedef bm_type::value_type book;
  33. bm_type bm;
  34. bm.insert(
  35. book( "Bjarne Stroustrup" , "The C++ Programming Language",
  36. "For C++ old-timers, the first edition of this book is"
  37. "the one that started it all—the font of our knowledge." )
  38. );
  39. // Print the author of the bible
  40. std::cout << bm.right.at("The C++ Programming Language");
  41. // Print the abstract of this book
  42. bm_type::left_iterator i = bm.left.find("Bjarne Stroustrup");
  43. std::cout << i->info;
  44. //]
  45. // Contrary to the two key types, the information will be mutable
  46. // using iterators.
  47. //[ code_tutorial_info_hook_mutable
  48. i->info += "More details about this book";
  49. //]
  50. // A new function is included in unique map views: info_at(key), that
  51. // mimics the standard at(key) function but returned the associated
  52. // information instead of the data.
  53. //[ code_tutorial_info_hook_info_at
  54. // Print the new abstract
  55. std::cout << bm.right.info_at("The C++ Programming Language");
  56. //]
  57. }
  58. struct author {};
  59. struct title {};
  60. struct abstract {};
  61. void tutorial_about_tagged_info_hook()
  62. {
  63. //[ code_tutorial_info_hook_tagged_info
  64. typedef bimap<
  65. multiset_of< tagged< std::string, author > >,
  66. set_of< tagged< std::string, title > >,
  67. with_info< tagged< std::string, abstract > >
  68. > bm_type;
  69. typedef bm_type::value_type book;
  70. bm_type bm;
  71. bm.insert(
  72. book( "Bjarne Stroustrup" , "The C++ Programming Language",
  73. "For C++ old-timers, the first edition of this book is"
  74. "the one that started it all—the font of our knowledge." )
  75. );
  76. // Print the author of the bible
  77. std::cout << bm.by<title>().at("The C++ Programming Language");
  78. // Print the abstract of this book
  79. bm_type::map_by<author>::iterator i = bm.by<author>().find("Bjarne Stroustrup");
  80. std::cout << i->get<abstract>();
  81. // Contrary to the two key types, the information will be mutable
  82. // using iterators.
  83. i->get<abstract>() += "More details about this book";
  84. // Print the new abstract
  85. std::cout << bm.by<title>().info_at("The C++ Programming Language");
  86. //]
  87. }
  88. void bimap_without_an_info_hook()
  89. {
  90. //[ code_tutorial_info_hook_nothing
  91. typedef bimap<
  92. multiset_of< std::string >, // author
  93. set_of< std::string > // title
  94. > bm_type;
  95. typedef bm_type::value_type book;
  96. bm_type bm;
  97. bm.insert( book( "Bjarne Stroustrup" , "The C++ Programming Language" ) );
  98. bm.insert( book( "Scott Meyers" , "Effective C++" ) );
  99. bm.insert( book( "Andrei Alexandrescu" , "Modern C++ Design" ) );
  100. // Print the author of Modern C++
  101. std::cout << bm.right.at( "Modern C++ Design" );
  102. //]
  103. }
  104. int main()
  105. {
  106. tutorial_about_info_hook();
  107. tutorial_about_tagged_info_hook();
  108. bimap_without_an_info_hook();
  109. return 0;
  110. }