population_bimap.cpp 3.5 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 <string>
  21. #include <boost/bimap/bimap.hpp>
  22. #include <boost/bimap/unordered_set_of.hpp>
  23. #include <boost/bimap/multiset_of.hpp>
  24. #include <boost/optional.hpp>
  25. #include <boost/none.hpp>
  26. #include <boost/foreach.hpp>
  27. #include <boost/assign/list_inserter.hpp>
  28. using namespace boost::bimaps;
  29. using namespace boost;
  30. using namespace std;
  31. int main()
  32. {
  33. {
  34. typedef bimap<
  35. string,
  36. multiset_of< optional<string> >
  37. > bm_type;
  38. bm_type bm;
  39. assign::insert( bm )
  40. ( "John" , string("lazarus" ) )
  41. ( "Peter", string("vinicius") )
  42. ( "Simon", string("vinicius") )
  43. ( "Brian", none )
  44. ;
  45. cout << "John is working in "
  46. << bm.left.at( "John" ).get_value_or( "no project" )
  47. << endl;
  48. cout << "Project vinicius is being developed by " << endl;
  49. BOOST_FOREACH( bm_type::right_reference rp,
  50. bm.right.equal_range( std::string("vinicius") ) )
  51. {
  52. cout << rp.second << endl;
  53. }
  54. cout << "This workers need a project " << endl;
  55. BOOST_FOREACH( bm_type::right_reference rp,
  56. bm.right.equal_range(none) )
  57. {
  58. cout << rp.second << endl;
  59. }
  60. }
  61. //[ code_population_bimap
  62. typedef bimap<
  63. unordered_set_of< std::string >,
  64. multiset_of< long, std::greater<long> >
  65. > population_bimap;
  66. typedef population_bimap::value_type population;
  67. population_bimap pop;
  68. pop.insert( population("China", 1321000000) );
  69. pop.insert( population("India", 1129000000) );
  70. pop.insert( population("United States", 301950000) );
  71. pop.insert( population("Indonesia", 234950000) );
  72. pop.insert( population("Brazil", 186500000) );
  73. pop.insert( population("Pakistan", 163630000) );
  74. std::cout << "Countries by their population:" << std::endl;
  75. // First requirement
  76. /*<< The right map view works like a
  77. `std::multimap< long, std::string, std::greater<long> >`,
  78. We can iterate over it to print the results in the required order. >>*/
  79. for( population_bimap::right_const_iterator
  80. i = pop.right.begin(), iend = pop.right.end();
  81. i != iend ; ++i )
  82. {
  83. std::cout << i->second << " with " << i->first << std::endl;
  84. }
  85. // Second requirement
  86. /*<< The left map view works like a `std::unordered_map< std::string, long >`,
  87. given the name of the country we can use it to search for the population
  88. in constant time >>*/
  89. std::cout << "Population of China: " << pop.left.at("China") << std::endl;
  90. //]
  91. return 0;
  92. }