Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / bimap / example / step_by_step.cpp
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 <iostream>
24 #include <cassert>
25
26 // A convinience header is avaiable in the boost directory:
27 #include <boost/bimap.hpp>
28
29 int main()
30 {
31     //[ code_step_by_step_definition
32
33     typedef boost::bimap< int, std::string > bm_type;
34     bm_type bm;
35     //]
36
37     //[ code_step_by_step_set_of_relations_view
38
39     bm.insert( bm_type::value_type(1, "one" ) );
40     bm.insert( bm_type::value_type(2, "two" ) );
41
42     std::cout << "There are " << bm.size() << "relations" << std::endl;
43
44     for( bm_type::const_iterator iter = bm.begin(), iend = bm.end(); 
45         iter != iend; ++iter )
46     {
47         // iter->left  : data : int
48         // iter->right : data : std::string
49
50         std::cout << iter->left << " <--> " << iter->right << std::endl;
51     }
52     //]
53
54     //[ code_step_by_step_left_map_view
55
56     /*<< The type of `bm.left` is `bm_type::left_map` and the type
57          of `bm.right` is `bm_type::right_map` >>*/
58     typedef bm_type::left_map::const_iterator left_const_iterator;
59
60     for( left_const_iterator left_iter = bm.left.begin(), iend = bm.left.end();
61          left_iter != iend; ++left_iter )
62     {
63         // left_iter->first  : key  : int
64         // left_iter->second : data : std::string
65
66         std::cout << left_iter->first << " --> " << left_iter->second << std::endl;
67     }
68
69     /*<< `bm_type::left_`\ -type- can be used as a shortcut for the more verbose
70     `bm_type::left_map::`\ -type- >>*/ 
71     bm_type::left_const_iterator left_iter = bm.left.find(2);
72     assert( left_iter->second ==  "two" );
73
74     /*<< This line produces the same effect of
75          `bm.insert( bm_type::value_type(3,"three") );` >>*/
76     bm.left.insert( bm_type::left_value_type( 3, "three" ) );
77     //]
78
79
80
81     //[ code_step_by_step_right_map_view
82
83     bm_type::right_const_iterator right_iter = bm.right.find("two");
84
85     // right_iter->first  : key  : std::string
86     // right_iter->second : data : int
87
88     assert( right_iter->second ==  2 );
89
90     assert( bm.right.at("one") == 1 );
91
92     bm.right.erase("two");
93
94     /*<< This line produces the same effect of
95          `bm.insert( bm_type::value_type(4,"four") );` >>*/
96     bm.right.insert( bm_type::right_value_type( "four", 4 ) );
97     //]
98
99     return 0;
100 }
101
102