simple_bimap.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_simple_bimap
  20. #include <string>
  21. #include <iostream>
  22. #include <boost/bimap.hpp>
  23. template< class MapType >
  24. void print_map(const MapType & map,
  25. const std::string & separator,
  26. std::ostream & os )
  27. {
  28. typedef typename MapType::const_iterator const_iterator;
  29. for( const_iterator i = map.begin(), iend = map.end(); i != iend; ++i )
  30. {
  31. os << i->first << separator << i->second << std::endl;
  32. }
  33. }
  34. int main()
  35. {
  36. // Soccer World cup
  37. typedef boost::bimap< std::string, int > results_bimap;
  38. typedef results_bimap::value_type position;
  39. results_bimap results;
  40. results.insert( position("Argentina" ,1) );
  41. results.insert( position("Spain" ,2) );
  42. results.insert( position("Germany" ,3) );
  43. results.insert( position("France" ,4) );
  44. std::cout << "The number of countries is " << results.size()
  45. << std::endl;
  46. std::cout << "The winner is " << results.right.at(1)
  47. << std::endl
  48. << std::endl;
  49. std::cout << "Countries names ordered by their final position:"
  50. << std::endl;
  51. // results.right works like a std::map< int, std::string >
  52. print_map( results.right, ") ", std::cout );
  53. std::cout << std::endl
  54. << "Countries names ordered alphabetically along with"
  55. "their final position:"
  56. << std::endl;
  57. // results.left works like a std::map< std::string, int >
  58. print_map( results.left, " ends in position ", std::cout );
  59. return 0;
  60. }
  61. //]