]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | //[ code_simple_bimap | |
24 | ||
25 | #include <string> | |
26 | #include <iostream> | |
27 | ||
28 | #include <boost/bimap.hpp> | |
29 | ||
30 | template< class MapType > | |
31 | void print_map(const MapType & map, | |
32 | const std::string & separator, | |
33 | std::ostream & os ) | |
34 | { | |
35 | typedef typename MapType::const_iterator const_iterator; | |
36 | ||
37 | for( const_iterator i = map.begin(), iend = map.end(); i != iend; ++i ) | |
38 | { | |
39 | os << i->first << separator << i->second << std::endl; | |
40 | } | |
41 | } | |
42 | ||
43 | int main() | |
44 | { | |
45 | // Soccer World cup | |
46 | ||
47 | typedef boost::bimap< std::string, int > results_bimap; | |
48 | typedef results_bimap::value_type position; | |
49 | ||
50 | results_bimap results; | |
51 | results.insert( position("Argentina" ,1) ); | |
52 | results.insert( position("Spain" ,2) ); | |
53 | results.insert( position("Germany" ,3) ); | |
54 | results.insert( position("France" ,4) ); | |
55 | ||
56 | std::cout << "The number of countries is " << results.size() | |
57 | << std::endl; | |
58 | ||
59 | std::cout << "The winner is " << results.right.at(1) | |
60 | << std::endl | |
61 | << std::endl; | |
62 | ||
63 | std::cout << "Countries names ordered by their final position:" | |
64 | << std::endl; | |
65 | ||
66 | // results.right works like a std::map< int, std::string > | |
67 | ||
68 | print_map( results.right, ") ", std::cout ); | |
69 | ||
70 | std::cout << std::endl | |
71 | << "Countries names ordered alphabetically along with" | |
72 | "their final position:" | |
73 | << std::endl; | |
74 | ||
75 | // results.left works like a std::map< std::string, int > | |
76 | ||
77 | print_map( results.left, " ends in position ", std::cout ); | |
78 | ||
79 | return 0; | |
80 | } | |
81 | //] | |
82 |