range.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <boost/range/functions.hpp>
  21. #include <boost/range/metafunctions.hpp>
  22. //[ code_bimap_and_boost_range_functions
  23. template< class ForwardReadableRange, class UnaryFunctor >
  24. UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func)
  25. {
  26. typedef typename
  27. boost::range_const_iterator<ForwardReadableRange>::type const_iterator;
  28. for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i )
  29. {
  30. func(*i);
  31. }
  32. return func;
  33. }
  34. template< class ForwardReadableRange, class Predicate >
  35. typename boost::range_difference<ForwardReadableRange>::type
  36. count_if(const ForwardReadableRange & r, Predicate pred)
  37. {
  38. typedef typename
  39. boost::range_const_iterator<ForwardReadableRange>::type const_iterator;
  40. typename boost::range_difference<ForwardReadableRange>::type c = 0;
  41. for( const_iterator i = boost::begin(r), iend = boost::end(r); i != iend; ++i )
  42. {
  43. if( pred(*i) ) ++c;
  44. }
  45. return c;
  46. }
  47. //]
  48. #include <boost/bimap/bimap.hpp>
  49. #include <boost/bimap/multiset_of.hpp>
  50. #include <boost/bimap/support/lambda.hpp>
  51. #include <boost/bind.hpp>
  52. using namespace boost::bimaps;
  53. using namespace boost;
  54. //[ code_bimap_and_boost_range
  55. struct pair_printer
  56. {
  57. pair_printer(std::ostream & o) : os(o) {}
  58. template< class Pair >
  59. void operator()(const Pair & p)
  60. {
  61. os << "(" << p.first << "," << p.second << ")";
  62. }
  63. private:
  64. std::ostream & os;
  65. };
  66. struct second_extractor
  67. {
  68. template< class Pair >
  69. const typename Pair::second_type & operator()(const Pair & p)
  70. {
  71. return p.second;
  72. }
  73. };
  74. int main()
  75. {
  76. typedef bimap< double, multiset_of<int> > bm_type;
  77. bm_type bm;
  78. bm.insert( bm_type::value_type(2.5 , 1) );
  79. bm.insert( bm_type::value_type(3.1 , 2) );
  80. //...
  81. bm.insert( bm_type::value_type(6.4 , 4) );
  82. bm.insert( bm_type::value_type(1.7 , 2) );
  83. // Print all the elements of the left map view
  84. for_each( bm.left, pair_printer(std::cout) );
  85. // Print a range of elements of the right map view
  86. for_each( bm.right.range( 2 <= _key, _key < 6 ), pair_printer(std::cout) );
  87. // Count the number of elements where the data is equal to 2 from a
  88. // range of elements of the left map view
  89. count_if( bm.left.range( 2.3 < _key, _key < 5.4 ),
  90. bind<int>( second_extractor(), _1 ) == 2 );
  91. return 0;
  92. }
  93. //]