unconstrained_collection.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <boost/bimap/unconstrained_set_of.hpp>
  24. #include <boost/bimap/support/lambda.hpp>
  25. using namespace boost::bimaps;
  26. int main()
  27. {
  28. // Boost.Bimap
  29. {
  30. //[ code_unconstrained_collection_bimap
  31. typedef bimap< std::string, unconstrained_set_of<int> > bm_type;
  32. typedef bm_type::left_map map_type;
  33. bm_type bm;
  34. map_type & m = bm.left;
  35. //]
  36. //[ code_unconstrained_collection_common
  37. m["one"] = 1;
  38. assert( m.find("one") != m.end() );
  39. for( map_type::iterator i = m.begin(), iend = m.end(); i != iend; ++i )
  40. {
  41. /*<< The right collection of the bimap is mutable so its elements
  42. can be modified using iterators. >>*/
  43. ++(i->second);
  44. }
  45. m.erase("one");
  46. //]
  47. m["one"] = 1;
  48. m["two"] = 2;
  49. //[ code_unconstrained_collection_only_for_bimap
  50. typedef map_type::const_iterator const_iterator;
  51. typedef std::pair<const_iterator,const_iterator> const_range;
  52. /*<< This range is a model of BidirectionalRange, read the docs of
  53. Boost.Range for more information. >>*/
  54. const_range r = m.range( "one" <= _key, _key <= "two" );
  55. for( const_iterator i = r.first; i != r.second; ++i )
  56. {
  57. std::cout << i->first << "-->" << i->second << std::endl;
  58. }
  59. m.modify_key( m.begin(), _key = "1" );
  60. //]
  61. }
  62. // Standard map
  63. {
  64. //[ code_unconstrained_collection_map
  65. typedef std::map< std::string, int > map_type;
  66. map_type m;
  67. //]
  68. }
  69. return 0;
  70. }