• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
9 //  VC++ 8.0 warns on usage of certain Standard Library and API functions that
10 //  can be cause buffer overruns or other possible security issues if misused.
11 //  See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
12 //  But the wording of the warning is misleading and unsettling, there are no
13 //  portable alternative functions, and VC++ 8.0's own libraries use the
14 //  functions in question. So turn off the warnings.
15 #define _CRT_SECURE_NO_DEPRECATE
16 #define _SCL_SECURE_NO_DEPRECATE
17 
18 // Boost.Bimap Example
19 //-----------------------------------------------------------------------------
20 
21 #include <boost/config.hpp>
22 
23 #include <iostream>
24 #include <string>
25 
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/unordered_set_of.hpp>
28 #include <boost/bimap/multiset_of.hpp>
29 
30 #include <boost/optional.hpp>
31 #include <boost/none.hpp>
32 #include <boost/foreach.hpp>
33 #include <boost/assign/list_inserter.hpp>
34 
35 using namespace boost::bimaps;
36 using namespace boost;
37 using namespace std;
38 
main()39 int main()
40 {
41     {
42 
43     typedef bimap<
44 
45         string,
46         multiset_of< optional<string> >
47 
48     > bm_type;
49 
50     bm_type bm;
51 
52     assign::insert( bm )
53 
54         ( "John" , string("lazarus" ) )
55         ( "Peter", string("vinicius") )
56         ( "Simon", string("vinicius") )
57         ( "Brian", none               )
58     ;
59 
60     cout << "John is working in "
61          << bm.left.at( "John" ).get_value_or( "no project" )
62          << endl;
63 
64     cout << "Project vinicius is being developed by " << endl;
65     BOOST_FOREACH( bm_type::right_reference rp,
66                    bm.right.equal_range( std::string("vinicius") ) )
67     {
68         cout << rp.second << endl;
69     }
70 
71     cout << "This workers need a project " << endl;
72     BOOST_FOREACH( bm_type::right_reference rp,
73                    bm.right.equal_range(none) )
74     {
75         cout << rp.second << endl;
76     }
77 
78 }
79 
80     //[ code_population_bimap
81 
82     typedef bimap<
83 
84         unordered_set_of< std::string >,
85         multiset_of< long, std::greater<long> >
86 
87     > population_bimap;
88 
89     typedef population_bimap::value_type population;
90 
91     population_bimap pop;
92     pop.insert( population("China",          1321000000) );
93     pop.insert( population("India",          1129000000) );
94     pop.insert( population("United States",   301950000) );
95     pop.insert( population("Indonesia",       234950000) );
96     pop.insert( population("Brazil",          186500000) );
97     pop.insert( population("Pakistan",        163630000) );
98 
99     std::cout << "Countries by their population:" << std::endl;
100 
101     // First requirement
102     /*<< The right map view works like a
103          `std::multimap< long, std::string, std::greater<long> >`,
104          We can iterate over it to print the results in the required order. >>*/
105     for( population_bimap::right_const_iterator
106             i = pop.right.begin(), iend = pop.right.end();
107             i != iend ; ++i )
108     {
109         std::cout << i->second << " with " << i->first << std::endl;
110     }
111 
112     // Second requirement
113     /*<< The left map view works like a `std::unordered_map< std::string, long >`,
114          given the name of the country we can use it to search for the population
115          in constant time >>*/
116     std::cout << "Population of China: " << pop.left.at("China") << std::endl;
117     //]
118 
119     return 0;
120 }
121 
122