| 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 | //[ code_tagged_simple_bimap |
| 24 | |
| 25 | #include <iostream> |
| 26 | |
| 27 | #include <boost/bimap.hpp> |
| 28 | |
| 29 | struct country {}; |
| 30 | struct place {}; |
| 31 | |
| 32 | int main() |
| 33 | { |
| 34 | using namespace boost::bimaps; |
| 35 | |
| 36 | // Soccer World cup. |
| 37 | |
| 38 | typedef bimap |
| 39 | < |
| 40 | tagged< std::string, country >, |
| 41 | tagged< int , place > |
| 42 | |
| 43 | > results_bimap; |
| 44 | |
| 45 | typedef results_bimap::value_type position; |
| 46 | |
| 47 | results_bimap results; |
| 48 | results.insert( x: position("Argentina" ,1) ); |
| 49 | results.insert( x: position("Spain" ,2) ); |
| 50 | results.insert( x: position("Germany" ,3) ); |
| 51 | results.insert( x: position("France" ,4) ); |
| 52 | |
| 53 | std::cout << "Countries names ordered by their final position:" |
| 54 | << std::endl; |
| 55 | |
| 56 | /*<< `results.by<place>()` is equivalent to `results.right` >>*/ |
| 57 | for( results_bimap::map_by<place>::const_iterator |
| 58 | i = results.by<place>().begin(), |
| 59 | iend = results.by<place>().end() ; |
| 60 | i != iend; ++i ) |
| 61 | { |
| 62 | /*<< `get<Tag>` works for each view of the bimap >>*/ |
| 63 | std::cout << i->get<place >() << ") " |
| 64 | << i->get<country>() << std::endl; |
| 65 | } |
| 66 | |
| 67 | std::cout << std::endl |
| 68 | << "Countries names ordered alfabetically along with" |
| 69 | "their final position:" |
| 70 | << std::endl; |
| 71 | |
| 72 | /*<< `results.by<country>()` is equivalent to `results.left` >>*/ |
| 73 | for( results_bimap::map_by<country>::const_iterator |
| 74 | i = results.by<country>().begin(), |
| 75 | iend = results.by<country>().end() ; |
| 76 | i != iend; ++i ) |
| 77 | { |
| 78 | std::cout << i->get<country>() << " ends " |
| 79 | << i->get<place >() << "ยบ" |
| 80 | << std::endl; |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | //] |
| 86 | |
| 87 | |