]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/bimap/example/population_bimap.cpp
3 // Copyright (c) 2006-2007 Matias Capeletto
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)
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
18 // Boost.Bimap Example
19 //-----------------------------------------------------------------------------
21 #include <boost/config.hpp>
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/unordered_set_of.hpp>
28 #include <boost/bimap/multiset_of.hpp>
30 #include <boost/optional.hpp>
31 #include <boost/none.hpp>
32 #include <boost/foreach.hpp>
33 #include <boost/assign/list_inserter.hpp>
35 using namespace boost::bimaps
;
36 using namespace boost
;
46 multiset_of
< optional
<string
> >
54 ( "John" , string("lazarus" ) )
55 ( "Peter", string("vinicius") )
56 ( "Simon", string("vinicius") )
60 cout
<< "John is working in "
61 << bm
.left
.at( "John" ).get_value_or( "no project" )
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") ) )
68 cout
<< rp
.second
<< endl
;
71 cout
<< "This workers need a project " << endl
;
72 BOOST_FOREACH( bm_type::right_reference rp
,
73 bm
.right
.equal_range(none
) )
75 cout
<< rp
.second
<< endl
;
80 //[ code_population_bimap
84 unordered_set_of
< std::string
>,
85 multiset_of
< long, std::greater
<long> >
89 typedef population_bimap::value_type population
;
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) );
99 std::cout
<< "Countries by their population:" << std::endl
;
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();
109 std::cout
<< i
->second
<< " with " << i
->first
<< std::endl
;
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
;