Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / bimap / example / tutorial_modify_and_replace.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 <string>
24 #include <iostream>
25
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/support/lambda.hpp>
28
29 using namespace boost::bimaps;
30
31 void test_replace()
32 {
33     //[ code_tutorial_replace
34
35     typedef bimap< int, std::string > bm_type;
36     bm_type bm;
37
38     bm.insert( bm_type::value_type(1,"one") );
39
40     // Replace (1,"one") with (1,"1") using the right map view
41     {
42         bm_type::right_iterator it = bm.right.find("one");
43
44         bool successful_replace = bm.right.replace_key( it, "1" );
45
46         assert( successful_replace );
47     }
48
49     bm.insert( bm_type::value_type(2,"two") );
50
51     // Fail to replace (1,"1") with (1,"two") using the left map view
52     {
53         assert( bm.size() == 2 );
54
55         bm_type::left_iterator it = bm.left.find(1);
56
57         bool successful_replace = bm.left.replace_data( it, "two" );
58
59         /*<< `it` is still valid here, and the bimap was left unchanged >>*/
60         assert( ! successful_replace );
61         assert( bm.size() == 2 );
62     }
63     //]
64 }
65
66 void test_modify()
67 {
68     //[ code_tutorial_modify
69
70     typedef bimap< int, std::string > bm_type;
71     bm_type bm;
72     bm.insert( bm_type::value_type(1,"one") );
73
74     // Modify (1,"one") to (1,"1") using the right map view
75     {
76         bm_type::right_iterator it = bm.right.find("one");
77
78         bool successful_modify = bm.right.modify_key( it , _key = "1" );
79
80         assert( successful_modify );
81     }
82
83     bm.insert( bm_type::value_type(2,"two") );
84
85     // Fail to modify (1,"1") to (1,"two") using the left map view
86     {
87         assert( bm.size() == 2 );
88
89         bm_type::left_iterator it = bm.left.find(1);
90
91         bool successful_modify = bm.left.modify_data( it, _data = "two" );
92
93         /*<< `it` is not longer valid and `(1,"1")` is removed from the bimap >>*/
94         assert( ! successful_modify );
95         assert( bm.size() == 1 );
96     }
97     //]
98
99     /*
100     // Modify (2,"two") to (3,"two") using the set of relations view
101     {
102         bm_type::iterator it = bm.begin();
103
104         bool successful_modify = bm.modify( it, ++_left );
105
106         assert( successful_modify );
107     }
108     */
109 }
110
111 int main()
112 {
113     test_replace();
114     test_modify();
115     return 0;
116 }
117
118