| 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 <string> |
| 24 | #include <iostream> |
| 25 | |
| 26 | #include <boost/foreach.hpp> |
| 27 | |
| 28 | #include <boost/bimap/bimap.hpp> |
| 29 | #include <boost/bimap/list_of.hpp> |
| 30 | #include <boost/bimap/support/lambda.hpp> |
| 31 | |
| 32 | using namespace boost::bimaps; |
| 33 | |
| 34 | |
| 35 | int main() |
| 36 | { |
| 37 | //[ code_bimap_and_boost_foreach |
| 38 | |
| 39 | typedef bimap< std::string, list_of<int> > bm_type; |
| 40 | |
| 41 | bm_type bm; |
| 42 | bm.insert( x: bm_type::value_type("1" , 1) ); |
| 43 | bm.insert( x: bm_type::value_type("2" , 2) ); |
| 44 | bm.insert( x: bm_type::value_type("3" , 4) ); |
| 45 | bm.insert( x: bm_type::value_type("4" , 2) ); |
| 46 | |
| 47 | BOOST_FOREACH( bm_type::left_reference p, bm.left ) |
| 48 | { |
| 49 | ++p.second; /*< We can modify the right element because we have |
| 50 | use a mutable collection type in the right side. >*/ |
| 51 | } |
| 52 | |
| 53 | BOOST_FOREACH( bm_type::right_const_reference p, bm.right ) |
| 54 | { |
| 55 | std::cout << p.first << "-->" << p.second << std::endl; |
| 56 | } |
| 57 | |
| 58 | //] |
| 59 | |
| 60 | // More examples |
| 61 | |
| 62 | BOOST_FOREACH( bm_type::right_reference p, bm.right ) |
| 63 | { |
| 64 | ++p.first; |
| 65 | } |
| 66 | |
| 67 | BOOST_FOREACH( bm_type::left_const_reference p, bm.left ) |
| 68 | { |
| 69 | std::cout << p.first << "-->" << p.second << std::endl; |
| 70 | } |
| 71 | |
| 72 | BOOST_FOREACH( bm_type::reference p, bm ) |
| 73 | { |
| 74 | ++p.right; |
| 75 | } |
| 76 | |
| 77 | const bm_type & cbm = bm; |
| 78 | BOOST_FOREACH( bm_type::const_reference p, cbm ) |
| 79 | { |
| 80 | std::cout << p.left << "-->" << p.right << std::endl; |
| 81 | } |
| 82 | |
| 83 | BOOST_FOREACH( bm_type::const_reference p, bm ) |
| 84 | { |
| 85 | std::cout << p.left << "-->" << p.right << std::endl; |
| 86 | } |
| 87 | |
| 88 | //[ code_bimap_and_boost_foreach_using_range |
| 89 | |
| 90 | BOOST_FOREACH( bm_type::left_reference p, |
| 91 | ( bm.left.range( std::string("1" ) <= _key, _key < std::string("3" ) ) )) |
| 92 | { |
| 93 | ++p.second; |
| 94 | } |
| 95 | |
| 96 | BOOST_FOREACH( bm_type::left_const_reference p, |
| 97 | ( bm.left.range( std::string("1" ) <= _key, _key < std::string("3" ) ) )) |
| 98 | { |
| 99 | std::cout << p.first << "-->" << p.second << std::endl; |
| 100 | } |
| 101 | //] |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | |