1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
// Boost.Bimap
//
// Copyright (c) 2006-2007 Matias Capeletto
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
// can be cause buffer overruns or other possible security issues if misused.
// See https://web.archive.org/web/20071014014301/http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
// But the wording of the warning is misleading and unsettling, there are no
// portable alternative functions, and VC++ 8.0's own libraries use the
// functions in question. So turn off the warnings.
#define _CRT_SECURE_NO_DEPRECATE
#define _SCL_SECURE_NO_DEPRECATE
// Boost.Bimap Example
//-----------------------------------------------------------------------------
#include <boost/config.hpp>
#include <string>
#include <iostream>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
using namespace boost::bimaps;
void untagged_version()
{
//[ code_user_defined_names_untagged_version
typedef bimap
<
multiset_of<std::string>,
int
> People;
People people;
// ...
int user_id;
std::cin >> user_id;
// people.right : map<id,name>
People::right_const_iterator id_iter = people.right.find(user_id);
if( id_iter != people.right.end() )
{
// first : id
// second : name
std::cout << "name: " << id_iter->second << std::endl
<< "id: " << id_iter->first << std::endl;
}
else
{
std::cout << "Unknown id, users are:" << std::endl;
// people.left : map<name,id>
for( People::left_const_iterator
name_iter = people.left.begin(),
iend = people.left.end();
name_iter != iend; ++name_iter )
{
// first : name
// second : id
std::cout << "name: " << name_iter->first << std::endl
<< "id: " << name_iter->second << std::endl;
}
}
//]
}
struct id {};
struct name {};
void tagged_version()
{
//[ code_user_defined_names_tagged_version
//<-
/*
//->
struct id {}; // Tag for the identification number
struct name {}; // Tag for the name of the person
//<-
*/
//->
typedef bimap
<
tagged< int , id > ,
multiset_of< tagged< std::string, name > >
> People;
People people;
// ...
int user_id;
std::cin >> user_id;
People::map_by<id>::const_iterator id_iter = people.by<id>().find(user_id);
if( id_iter != people.by<id>().end() )
{
std::cout << "name: " << id_iter->get<name>() << std::endl
<< "id: " << id_iter->get<id>() << std::endl;
}
else
{
std::cout << "Unknown id, users are:" << std::endl;
for( People::map_by<name>::const_iterator
name_iter = people.by<name>().begin(),
iend = people.by<name>().end();
name_iter != iend; ++name_iter )
{
std::cout << "name: " << name_iter->get<name>() << std::endl
<< "id: " << name_iter->get<id>() << std::endl;
}
}
//]
}
int main()
{
untagged_version();
tagged_version();
return 0;
}
|