]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/bimap/example/tutorial_modify_and_replace.cpp
3 // Copyright (c) 2006-2007 Matias Capeletto
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)
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
18 // Boost.Bimap Example
19 //-----------------------------------------------------------------------------
21 #include <boost/config.hpp>
26 #include <boost/bimap/bimap.hpp>
27 #include <boost/bimap/support/lambda.hpp>
29 using namespace boost::bimaps
;
33 //[ code_tutorial_replace
35 typedef bimap
< int, std::string
> bm_type
;
38 bm
.insert( bm_type::value_type(1,"one") );
40 // Replace (1,"one") with (1,"1") using the right map view
42 bm_type::right_iterator it
= bm
.right
.find("one");
44 bool successful_replace
= bm
.right
.replace_key( it
, "1" );
46 assert( successful_replace
);
49 bm
.insert( bm_type::value_type(2,"two") );
51 // Fail to replace (1,"1") with (1,"two") using the left map view
53 assert( bm
.size() == 2 );
55 bm_type::left_iterator it
= bm
.left
.find(1);
57 bool successful_replace
= bm
.left
.replace_data( it
, "two" );
59 /*<< `it` is still valid here, and the bimap was left unchanged >>*/
60 assert( ! successful_replace
);
61 assert( bm
.size() == 2 );
68 //[ code_tutorial_modify
70 typedef bimap
< int, std::string
> bm_type
;
72 bm
.insert( bm_type::value_type(1,"one") );
74 // Modify (1,"one") to (1,"1") using the right map view
76 bm_type::right_iterator it
= bm
.right
.find("one");
78 bool successful_modify
= bm
.right
.modify_key( it
, _key
= "1" );
80 assert( successful_modify
);
83 bm
.insert( bm_type::value_type(2,"two") );
85 // Fail to modify (1,"1") to (1,"two") using the left map view
87 assert( bm
.size() == 2 );
89 bm_type::left_iterator it
= bm
.left
.find(1);
91 bool successful_modify
= bm
.left
.modify_data( it
, _data
= "two" );
93 /*<< `it` is not longer valid and `(1,"1")` is removed from the bimap >>*/
94 assert( ! successful_modify
);
95 assert( bm
.size() == 1 );
100 // Modify (2,"two") to (3,"two") using the set of relations view
102 bm_type::iterator it = bm.begin();
104 bool successful_modify = bm.modify( it, ++_left );
106 assert( successful_modify );