step_by_step.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 <iostream>
  20. #include <cassert>
  21. // A convinience header is avaiable in the boost directory:
  22. #include <boost/bimap.hpp>
  23. int main()
  24. {
  25. //[ code_step_by_step_definition
  26. typedef boost::bimap< int, std::string > bm_type;
  27. bm_type bm;
  28. //]
  29. //[ code_step_by_step_set_of_relations_view
  30. bm.insert( bm_type::value_type(1, "one" ) );
  31. bm.insert( bm_type::value_type(2, "two" ) );
  32. std::cout << "There are " << bm.size() << "relations" << std::endl;
  33. for( bm_type::const_iterator iter = bm.begin(), iend = bm.end();
  34. iter != iend; ++iter )
  35. {
  36. // iter->left : data : int
  37. // iter->right : data : std::string
  38. std::cout << iter->left << " <--> " << iter->right << std::endl;
  39. }
  40. //]
  41. //[ code_step_by_step_left_map_view
  42. /*<< The type of `bm.left` is `bm_type::left_map` and the type
  43. of `bm.right` is `bm_type::right_map` >>*/
  44. typedef bm_type::left_map::const_iterator left_const_iterator;
  45. for( left_const_iterator left_iter = bm.left.begin(), iend = bm.left.end();
  46. left_iter != iend; ++left_iter )
  47. {
  48. // left_iter->first : key : int
  49. // left_iter->second : data : std::string
  50. std::cout << left_iter->first << " --> " << left_iter->second << std::endl;
  51. }
  52. /*<< `bm_type::left_`\ -type- can be used as a shortcut for the more verbose
  53. `bm_type::left_map::`\ -type- >>*/
  54. bm_type::left_const_iterator left_iter = bm.left.find(2);
  55. assert( left_iter->second == "two" );
  56. /*<< This line produces the same effect of
  57. `bm.insert( bm_type::value_type(3,"three") );` >>*/
  58. bm.left.insert( bm_type::left_value_type( 3, "three" ) );
  59. //]
  60. //[ code_step_by_step_right_map_view
  61. bm_type::right_const_iterator right_iter = bm.right.find("two");
  62. // right_iter->first : key : std::string
  63. // right_iter->second : data : int
  64. assert( right_iter->second == 2 );
  65. assert( bm.right.at("one") == 1 );
  66. bm.right.erase("two");
  67. /*<< This line produces the same effect of
  68. `bm.insert( bm_type::value_type(4,"four") );` >>*/
  69. bm.right.insert( bm_type::right_value_type( "four", 4 ) );
  70. //]
  71. return 0;
  72. }