Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / bimap / example / unconstrained_collection.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 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 <map>
27 #include <boost/bimap/bimap.hpp>
28 #include <boost/bimap/unconstrained_set_of.hpp>
29 #include <boost/bimap/support/lambda.hpp>
30
31 using namespace boost::bimaps;
32
33 int main()
34 {
35     // Boost.Bimap
36     {
37     //[ code_unconstrained_collection_bimap
38
39     typedef bimap< std::string, unconstrained_set_of<int> > bm_type;
40     typedef bm_type::left_map map_type;
41
42     bm_type bm;
43     map_type & m = bm.left;
44     //]
45
46     //[ code_unconstrained_collection_common
47
48     m["one"] = 1;
49
50     assert( m.find("one") != m.end() );
51
52     for( map_type::iterator i = m.begin(), iend = m.end(); i != iend; ++i )
53     {
54         /*<< The right collection of the bimap is mutable so its elements 
55              can be modified using iterators. >>*/
56         ++(i->second);
57     }
58
59     m.erase("one");
60     //]
61
62     m["one"] = 1;
63     m["two"] = 2;
64
65     //[ code_unconstrained_collection_only_for_bimap
66     typedef map_type::const_iterator const_iterator;
67     typedef std::pair<const_iterator,const_iterator> const_range;
68
69     /*<< This range is a model of BidirectionalRange, read the docs of 
70          Boost.Range for more information. >>*/
71     const_range r = m.range( "one" <= _key, _key <= "two" );
72     for( const_iterator i = r.first; i != r.second; ++i )
73     {
74         std::cout << i->first << "-->" << i->second << std::endl;
75     }
76
77     m.modify_key( m.begin(), _key = "1" );
78     //]
79     }
80
81     // Standard map
82     {
83     //[ code_unconstrained_collection_map
84
85     typedef std::map< std::string, int > map_type;
86
87     map_type m;
88     //]
89     }
90
91     return 0;
92 }
93
94