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 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 | |
25 | #include <boost/range/functions.hpp> |
26 | #include <boost/range/metafunctions.hpp> |
27 | |
28 | //[ code_bimap_and_boost_range_functions |
29 | |
30 | template< class ForwardReadableRange, class UnaryFunctor > |
31 | UnaryFunctor for_each(const ForwardReadableRange & r, UnaryFunctor func) |
32 | { |
33 | typedef typename |
34 | boost::range_const_iterator<ForwardReadableRange>::type const_iterator; |
35 | |
36 | for(const_iterator i= boost::begin(r), iend= boost::end(r); i!=iend; ++i ) |
37 | { |
38 | func(*i); |
39 | } |
40 | |
41 | return func; |
42 | } |
43 | |
44 | template< class ForwardReadableRange, class Predicate > |
45 | typename boost::range_difference<ForwardReadableRange>::type |
46 | count_if(const ForwardReadableRange & r, Predicate pred) |
47 | { |
48 | typedef typename |
49 | boost::range_const_iterator<ForwardReadableRange>::type const_iterator; |
50 | |
51 | typename boost::range_difference<ForwardReadableRange>::type c = 0; |
52 | |
53 | for( const_iterator i = boost::begin(r), iend = boost::end(r); i != iend; ++i ) |
54 | { |
55 | if( pred(*i) ) ++c; |
56 | } |
57 | |
58 | return c; |
59 | } |
60 | //] |
61 | |
62 | #include <boost/bimap/bimap.hpp> |
63 | #include <boost/bimap/multiset_of.hpp> |
64 | #include <boost/bimap/support/lambda.hpp> |
65 | #include <boost/bind.hpp> |
66 | |
67 | using namespace boost::bimaps; |
68 | using namespace boost; |
69 | |
70 | //[ code_bimap_and_boost_range |
71 | |
72 | struct pair_printer |
73 | { |
74 | pair_printer(std::ostream & o) : os(o) {} |
75 | template< class Pair > |
76 | void operator()(const Pair & p) |
77 | { |
78 | os << "(" << p.first << "," << p.second << ")" ; |
79 | } |
80 | private: |
81 | std::ostream & os; |
82 | }; |
83 | |
84 | struct |
85 | { |
86 | template< class Pair > |
87 | const typename Pair::second_type & (const Pair & p) |
88 | { |
89 | return p.second; |
90 | } |
91 | }; |
92 | |
93 | int main() |
94 | { |
95 | typedef bimap< double, multiset_of<int> > bm_type; |
96 | |
97 | bm_type bm; |
98 | bm.insert( x: bm_type::value_type(2.5 , 1) ); |
99 | bm.insert( x: bm_type::value_type(3.1 , 2) ); |
100 | //... |
101 | bm.insert( x: bm_type::value_type(6.4 , 4) ); |
102 | bm.insert( x: bm_type::value_type(1.7 , 2) ); |
103 | |
104 | // Print all the elements of the left map view |
105 | |
106 | for_each( r: bm.left, func: pair_printer(std::cout) ); |
107 | |
108 | // Print a range of elements of the right map view |
109 | |
110 | for_each( r: bm.right.range( lower: 2 <= _key, upper: _key < 6 ), func: pair_printer(std::cout) ); |
111 | |
112 | // Count the number of elements where the data is equal to 2 from a |
113 | // range of elements of the left map view |
114 | |
115 | count_if( r: bm.left.range( lower: 2.3 < _key, upper: _key < 5.4 ), |
116 | pred: bind<int>( f: second_extractor(), a1: _1 ) == 2 ); |
117 | |
118 | return 0; |
119 | } |
120 | //] |
121 | |
122 | |