tagged_simple_bimap.cpp 2.5 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. //[ code_tagged_simple_bimap
  20. #include <iostream>
  21. #include <boost/bimap.hpp>
  22. struct country {};
  23. struct place {};
  24. int main()
  25. {
  26. using namespace boost::bimaps;
  27. // Soccer World cup.
  28. typedef bimap
  29. <
  30. tagged< std::string, country >,
  31. tagged< int , place >
  32. > results_bimap;
  33. typedef results_bimap::value_type position;
  34. results_bimap results;
  35. results.insert( position("Argentina" ,1) );
  36. results.insert( position("Spain" ,2) );
  37. results.insert( position("Germany" ,3) );
  38. results.insert( position("France" ,4) );
  39. std::cout << "Countries names ordered by their final position:"
  40. << std::endl;
  41. /*<< `results.by<place>()` is equivalent to `results.right` >>*/
  42. for( results_bimap::map_by<place>::const_iterator
  43. i = results.by<place>().begin(),
  44. iend = results.by<place>().end() ;
  45. i != iend; ++i )
  46. {
  47. /*<< `get<Tag>` works for each view of the bimap >>*/
  48. std::cout << i->get<place >() << ") "
  49. << i->get<country>() << std::endl;
  50. }
  51. std::cout << std::endl
  52. << "Countries names ordered alfabetically along with"
  53. "their final position:"
  54. << std::endl;
  55. /*<< `results.by<country>()` is equivalent to `results.left` >>*/
  56. for( results_bimap::map_by<country>::const_iterator
  57. i = results.by<country>().begin(),
  58. iend = results.by<country>().end() ;
  59. i != iend; ++i )
  60. {
  61. std::cout << i->get<country>() << " ends "
  62. << i->get<place >() << "º"
  63. << std::endl;
  64. }
  65. return 0;
  66. }
  67. //]