typeof.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/typeof/typeof.hpp>
  23. using namespace boost::bimaps;
  24. struct name {};
  25. struct number {};
  26. void using_auto()
  27. {
  28. //[ code_bimap_and_boost_typeof_first
  29. typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type;
  30. bm_type bm;
  31. bm.insert( bm_type::value_type("one" ,1) );
  32. bm.insert( bm_type::value_type("two" ,2) );
  33. //]
  34. //[ code_bimap_and_boost_typeof_using_auto
  35. for( BOOST_AUTO(iter, bm.by<name>().begin()); iter!=bm.by<name>().end(); ++iter)
  36. {
  37. std::cout << iter->first << " --> " << iter->second << std::endl;
  38. }
  39. BOOST_AUTO( iter, bm.by<number>().find(2) );
  40. std::cout << "2: " << iter->get<name>();
  41. //]
  42. }
  43. void not_using_auto()
  44. {
  45. typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type;
  46. bm_type bm;
  47. bm.insert( bm_type::value_type("one" ,1) );
  48. bm.insert( bm_type::value_type("two" ,2) );
  49. //[ code_bimap_and_boost_typeof_not_using_auto
  50. for( bm_type::map_by<name>::iterator iter = bm.by<name>().begin();
  51. iter!=bm.by<name>().end(); ++iter)
  52. {
  53. std::cout << iter->first << " --> " << iter->second << std::endl;
  54. }
  55. bm_type::map_by<number>::iterator iter = bm.by<number>().find(2);
  56. std::cout << "2: " << iter->get<name>();
  57. //]
  58. }
  59. int main()
  60. {
  61. using_auto();
  62. not_using_auto();
  63. return 0;
  64. }