tutorial_range.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 using_upper_and_lower_bound()
  25. {
  26. //[ code_tutorial_range_standard_way
  27. typedef bimap<int,std::string> bm_type;
  28. bm_type bm;
  29. // ...
  30. bm_type::left_iterator iter_first = bm.left.lower_bound(20);
  31. bm_type::left_iterator iter_second = bm.left.upper_bound(50);
  32. // range [iter_first,iter_second) contains the elements in [20,50]
  33. //]
  34. // Subtle changes
  35. {
  36. //[ code_tutorial_range_standard_way_subtle_changes
  37. bm_type::left_iterator iter_first = bm.left.upper_bound(20);
  38. bm_type::left_iterator iter_second = bm.left.lower_bound(50);
  39. // range [iter_first,iter_second) contains the elements in (20,50)
  40. //]
  41. }
  42. }
  43. void using_range()
  44. {
  45. //[ code_tutorial_range
  46. typedef bimap<int,std::string> bm_type;
  47. bm_type bm;
  48. // ...
  49. /*<< `range_type` is a handy typedef equal to `std::pair<iterator,iterator>`.
  50. `const_range_type` is provided too, and it is equal to
  51. `std::pair<const_iterator,const_iterator>` >>*/
  52. bm_type::left_range_type r;
  53. /*<< _key is a Boost.Lambda placeholder. To use it you have to include
  54. `<boost/bimap/support/lambda.hpp>` >>*/
  55. r = bm.left.range( 20 <= _key, _key <= 50 ); // [20,50]
  56. r = bm.left.range( 20 < _key, _key < 50 ); // (20,50)
  57. r = bm.left.range( 20 <= _key, _key < 50 ); // [20,50)
  58. //]
  59. //[ code_tutorial_range_unbounded
  60. r = bm.left.range( 20 <= _key, unbounded ); // [20,inf)
  61. r = bm.left.range( unbounded , _key < 50 ); // (-inf,50)
  62. /*<< This is equivalent to std::make_pair(s.begin(),s.end()) >>*/
  63. r = bm.left.range( unbounded , unbounded ); // (-inf,inf)
  64. //]
  65. }
  66. int main()
  67. {
  68. using_upper_and_lower_bound();
  69. using_range();
  70. return 0;
  71. }