1
/* Boost.MultiIndex basic example.
3
* Copyright 2003-2008 Joaquin M Lopez Munoz.
4
* Distributed under the Boost Software License, Version 1.0.
5
* (See accompanying file LICENSE_1_0.txt or copy at
6
* http://www.boost.org/LICENSE_1_0.txt)
8
* See http://www.boost.org/libs/multi_index for library home page.
12
#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
13
#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
16
#include <boost/multi_index_container.hpp>
17
#include <boost/multi_index/member.hpp>
18
#include <boost/multi_index/ordered_index.hpp>
24
using boost::multi_index_container;
25
using namespace boost::multi_index;
27
/* an employee record holds its ID, name and age */
35
employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
37
friend std::ostream& operator<<(std::ostream& os,const employee& e)
39
os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
44
/* tags for accessing the corresponding indices of employee_set */
50
/* see Compiler specifics: Use of member_offset for info on
51
* BOOST_MULTI_INDEX_MEMBER
54
/* Define a multi_index_container of employees with following indices:
55
* - a unique index sorted by employee::int,
56
* - a non-unique index sorted by employee::name,
57
* - a non-unique index sorted by employee::age.
60
typedef multi_index_container<
64
tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
66
tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
68
tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
71
template<typename Tag,typename MultiIndexContainer>
73
const MultiIndexContainer& s,
74
Tag* =0 /* fixes a MSVC++ 6.0 bug with implicit template function parms */
77
/* obtain a reference to the index tagged by Tag */
79
const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& i=
82
typedef typename MultiIndexContainer::value_type value_type;
84
/* dump the elements of the index to cout */
86
std::copy(i.begin(),i.end(),std::ostream_iterator<value_type>(std::cout));
94
es.insert(employee(0,"Joe",31));
95
es.insert(employee(1,"Robert",27));
96
es.insert(employee(2,"John",40));
98
/* next insertion will fail, as there is an employee with
102
es.insert(employee(2,"Aristotle",2387));
104
es.insert(employee(3,"Albert",20));
105
es.insert(employee(4,"John",57));
107
/* list the employees sorted by ID, name and age */
109
std::cout<<"by ID"<<std::endl;
110
print_out_by<id>(es);
111
std::cout<<std::endl;
113
std::cout<<"by name"<<std::endl;
114
print_out_by<name>(es);
115
std::cout<<std::endl;
117
std::cout<<"by age"<<std::endl;
118
print_out_by<age>(es);
119
std::cout<<std::endl;