standard_map_comparison.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 <map>
  22. #include <boost/bimap/bimap.hpp>
  23. using namespace boost::bimaps;
  24. //[ code_standard_map_comparison
  25. template< class Map, class CompatibleKey, class CompatibleData >
  26. void use_it( Map & m,
  27. const CompatibleKey & key,
  28. const CompatibleData & data )
  29. {
  30. typedef typename Map::value_type value_type;
  31. typedef typename Map::const_iterator const_iterator;
  32. m.insert( value_type(key,data) );
  33. const_iterator iter = m.find(key);
  34. if( iter != m.end() )
  35. {
  36. assert( iter->first == key );
  37. assert( iter->second == data );
  38. std::cout << iter->first << " --> " << iter->second;
  39. }
  40. m.erase(key);
  41. }
  42. int main()
  43. {
  44. typedef bimap< set_of<std::string>, set_of<int> > bimap_type;
  45. bimap_type bm;
  46. // Standard map
  47. {
  48. typedef std::map< std::string, int > map_type;
  49. map_type m;
  50. use_it( m, "one", 1 );
  51. }
  52. // Left map view
  53. {
  54. typedef bimap_type::left_map map_type;
  55. map_type & m = bm.left;
  56. use_it( m, "one", 1 );
  57. }
  58. // Reverse standard map
  59. {
  60. typedef std::map< int, std::string > reverse_map_type;
  61. reverse_map_type rm;
  62. use_it( rm, 1, "one" );
  63. }
  64. // Right map view
  65. {
  66. typedef bimap_type::right_map reverse_map_type;
  67. reverse_map_type & rm = bm.right;
  68. use_it( rm, 1, "one" );
  69. }
  70. return 0;
  71. }
  72. //]