tutorial_modify_and_replace.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/support/lambda.hpp>
  23. using namespace boost::bimaps;
  24. void test_replace()
  25. {
  26. //[ code_tutorial_replace
  27. typedef bimap< int, std::string > bm_type;
  28. bm_type bm;
  29. bm.insert( bm_type::value_type(1,"one") );
  30. // Replace (1,"one") with (1,"1") using the right map view
  31. {
  32. bm_type::right_iterator it = bm.right.find("one");
  33. bool successful_replace = bm.right.replace_key( it, "1" );
  34. assert( successful_replace );
  35. }
  36. bm.insert( bm_type::value_type(2,"two") );
  37. // Fail to replace (1,"1") with (1,"two") using the left map view
  38. {
  39. assert( bm.size() == 2 );
  40. bm_type::left_iterator it = bm.left.find(1);
  41. bool successful_replace = bm.left.replace_data( it, "two" );
  42. /*<< `it` is still valid here, and the bimap was left unchanged >>*/
  43. assert( ! successful_replace );
  44. assert( bm.size() == 2 );
  45. }
  46. //]
  47. }
  48. void test_modify()
  49. {
  50. //[ code_tutorial_modify
  51. typedef bimap< int, std::string > bm_type;
  52. bm_type bm;
  53. bm.insert( bm_type::value_type(1,"one") );
  54. // Modify (1,"one") to (1,"1") using the right map view
  55. {
  56. bm_type::right_iterator it = bm.right.find("one");
  57. bool successful_modify = bm.right.modify_key( it , _key = "1" );
  58. assert( successful_modify );
  59. }
  60. bm.insert( bm_type::value_type(2,"two") );
  61. // Fail to modify (1,"1") to (1,"two") using the left map view
  62. {
  63. assert( bm.size() == 2 );
  64. bm_type::left_iterator it = bm.left.find(1);
  65. bool successful_modify = bm.left.modify_data( it, _data = "two" );
  66. /*<< `it` is not longer valid and `(1,"1")` is removed from the bimap >>*/
  67. assert( ! successful_modify );
  68. assert( bm.size() == 1 );
  69. }
  70. //]
  71. /*
  72. // Modify (2,"two") to (3,"two") using the set of relations view
  73. {
  74. bm_type::iterator it = bm.begin();
  75. bool successful_modify = bm.modify( it, ++_left );
  76. assert( successful_modify );
  77. }
  78. */
  79. }
  80. int main()
  81. {
  82. test_replace();
  83. test_modify();
  84. return 0;
  85. }