Imported Upstream version 1.72.0
[platform/upstream/boost.git] / libs / bimap / example / user_defined_names.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/multiset_of.hpp>
28
29 using namespace boost::bimaps;
30
31 void untagged_version()
32 {
33     //[ code_user_defined_names_untagged_version
34
35     typedef bimap
36     <
37         multiset_of<std::string>,
38         int
39
40     > People;
41
42     People people;
43
44     // ...
45
46     int user_id;
47     std::cin >> user_id;
48
49     // people.right : map<id,name>
50
51     People::right_const_iterator id_iter = people.right.find(user_id);
52     if( id_iter != people.right.end() )
53     {
54         // first  : id
55         // second : name
56
57         std::cout << "name: " << id_iter->second << std::endl
58                   << "id:   " << id_iter->first  << std::endl;
59     }
60     else
61     {
62         std::cout << "Unknown id, users are:" << std::endl;
63
64         // people.left : map<name,id>
65
66         for( People::left_const_iterator
67                 name_iter = people.left.begin(),
68                 iend      = people.left.end();
69
70              name_iter != iend; ++name_iter )
71         {
72             // first  : name
73             // second : id
74
75             std::cout << "name: " << name_iter->first  << std::endl
76                       << "id:   " << name_iter->second << std::endl;
77         }
78     }
79     //]
80 }
81
82 struct id   {};
83 struct name {};
84
85 void tagged_version()
86 {
87     //[ code_user_defined_names_tagged_version
88
89     //<-
90     /*
91     //->
92     struct id   {}; // Tag for the identification number
93     struct name {}; // Tag for the name of the person
94     //<-
95     */
96     //->
97
98     typedef bimap
99     <
100                      tagged< int        , id   >  ,
101         multiset_of< tagged< std::string, name > >
102
103     > People;
104
105     People people;
106
107     // ...
108
109     int user_id;
110     std::cin >> user_id;
111
112     People::map_by<id>::const_iterator id_iter = people.by<id>().find(user_id);
113     if( id_iter != people.by<id>().end() )
114     {
115         std::cout << "name: " << id_iter->get<name>() << std::endl
116                   << "id:   " << id_iter->get<id>()   << std::endl;
117     }
118     else
119     {
120         std::cout << "Unknown id, users are:" << std::endl;
121
122         for( People::map_by<name>::const_iterator
123                 name_iter = people.by<name>().begin(),
124                 iend      = people.by<name>().end();
125
126             name_iter != iend; ++name_iter )
127         {
128             std::cout << "name: " << name_iter->get<name>() << std::endl
129                       << "id:   " << name_iter->get<id>()   << std::endl;
130         }
131     }
132     //]
133 }
134
135 int main()
136 {
137     untagged_version();
138     tagged_version();
139
140     return 0;
141 }
142