| 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 | #include <boost/bimap/bimap.hpp> |
| 26 | |
| 27 | #include <boost/typeof/typeof.hpp> |
| 28 | |
| 29 | using namespace boost::bimaps; |
| 30 | |
| 31 | struct name {}; |
| 32 | struct number {}; |
| 33 | |
| 34 | void using_auto() |
| 35 | { |
| 36 | //[ code_bimap_and_boost_typeof_first |
| 37 | |
| 38 | typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type; |
| 39 | bm_type bm; |
| 40 | bm.insert( x: bm_type::value_type("one" ,1) ); |
| 41 | bm.insert( x: bm_type::value_type("two" ,2) ); |
| 42 | //] |
| 43 | |
| 44 | //[ code_bimap_and_boost_typeof_using_auto |
| 45 | |
| 46 | for( BOOST_AUTO(iter, bm.by<name>().begin()); iter!=bm.by<name>().end(); ++iter) |
| 47 | { |
| 48 | std::cout << iter->first << " --> " << iter->second << std::endl; |
| 49 | } |
| 50 | |
| 51 | BOOST_AUTO( iter, bm.by<number>().find(2) ); |
| 52 | std::cout << "2: " << iter->get<name>(); |
| 53 | //] |
| 54 | } |
| 55 | |
| 56 | void not_using_auto() |
| 57 | { |
| 58 | typedef bimap< tagged<std::string,name>, tagged<int,number> > bm_type; |
| 59 | bm_type bm; |
| 60 | bm.insert( x: bm_type::value_type("one" ,1) ); |
| 61 | bm.insert( x: bm_type::value_type("two" ,2) ); |
| 62 | |
| 63 | //[ code_bimap_and_boost_typeof_not_using_auto |
| 64 | |
| 65 | for( bm_type::map_by<name>::iterator iter = bm.by<name>().begin(); |
| 66 | iter!=bm.by<name>().end(); ++iter) |
| 67 | { |
| 68 | std::cout << iter->first << " --> " << iter->second << std::endl; |
| 69 | } |
| 70 | |
| 71 | bm_type::map_by<number>::iterator iter = bm.by<number>().find(k: 2); |
| 72 | std::cout << "2: " << iter->get<name>(); |
| 73 | //] |
| 74 | } |
| 75 | |
| 76 | int main() |
| 77 | { |
| 78 | using_auto(); |
| 79 | not_using_auto(); |
| 80 | |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |