foreach.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/foreach.hpp>
  22. #include <boost/bimap/bimap.hpp>
  23. #include <boost/bimap/list_of.hpp>
  24. #include <boost/bimap/support/lambda.hpp>
  25. using namespace boost::bimaps;
  26. int main()
  27. {
  28. //[ code_bimap_and_boost_foreach
  29. typedef bimap< std::string, list_of<int> > bm_type;
  30. bm_type bm;
  31. bm.insert( bm_type::value_type("1", 1) );
  32. bm.insert( bm_type::value_type("2", 2) );
  33. bm.insert( bm_type::value_type("3", 4) );
  34. bm.insert( bm_type::value_type("4", 2) );
  35. BOOST_FOREACH( bm_type::left_reference p, bm.left )
  36. {
  37. ++p.second; /*< We can modify the right element because we have
  38. use a mutable collection type in the right side. >*/
  39. }
  40. BOOST_FOREACH( bm_type::right_const_reference p, bm.right )
  41. {
  42. std::cout << p.first << "-->" << p.second << std::endl;
  43. }
  44. //]
  45. // More examples
  46. BOOST_FOREACH( bm_type::right_reference p, bm.right )
  47. {
  48. ++p.first;
  49. }
  50. BOOST_FOREACH( bm_type::left_const_reference p, bm.left )
  51. {
  52. std::cout << p.first << "-->" << p.second << std::endl;
  53. }
  54. BOOST_FOREACH( bm_type::reference p, bm )
  55. {
  56. ++p.right;
  57. }
  58. const bm_type & cbm = bm;
  59. BOOST_FOREACH( bm_type::const_reference p, cbm )
  60. {
  61. std::cout << p.left << "-->" << p.right << std::endl;
  62. }
  63. BOOST_FOREACH( bm_type::const_reference p, bm )
  64. {
  65. std::cout << p.left << "-->" << p.right << std::endl;
  66. }
  67. //[ code_bimap_and_boost_foreach_using_range
  68. BOOST_FOREACH( bm_type::left_reference p,
  69. ( bm.left.range( std::string("1") <= _key, _key < std::string("3") ) ))
  70. {
  71. ++p.second;
  72. }
  73. BOOST_FOREACH( bm_type::left_const_reference p,
  74. ( bm.left.range( std::string("1") <= _key, _key < std::string("3") ) ))
  75. {
  76. std::cout << p.first << "-->" << p.second << std::endl;
  77. }
  78. //]
  79. return 0;
  80. }