blob: f7c46ced0e23ca6ab3956df995f7354775380186 [file] [log] [blame]
Brian Silverman3cbbaca2018-08-04 23:38:07 -07001/* Boost.MultiIndex basic example.
2 *
3 * Copyright 2003-2013 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)
7 *
8 * See http://www.boost.org/libs/multi_index for library home page.
9 */
10
11#if !defined(NDEBUG)
12#define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
13#define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
14#endif
15
16#include <boost/multi_index_container.hpp>
17#include <boost/multi_index/member.hpp>
18#include <boost/multi_index/ordered_index.hpp>
19#include <algorithm>
20#include <iostream>
21#include <iterator>
22#include <string>
23
24using boost::multi_index_container;
25using namespace boost::multi_index;
26
27/* an employee record holds its ID, name and age */
28
29struct employee
30{
31 int id;
32 std::string name;
33 int age;
34
35 employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
36
37 friend std::ostream& operator<<(std::ostream& os,const employee& e)
38 {
39 os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
40 return os;
41 }
42};
43
44/* tags for accessing the corresponding indices of employee_set */
45
46struct id{};
47struct name{};
48struct age{};
49
50/* see Compiler specifics: Use of member_offset for info on
51 * BOOST_MULTI_INDEX_MEMBER
52 */
53
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.
58 */
59
60typedef multi_index_container<
61 employee,
62 indexed_by<
63 ordered_unique<
64 tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
65 ordered_non_unique<
66 tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
67 ordered_non_unique<
68 tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
69> employee_set;
70
71template<typename Tag,typename MultiIndexContainer>
72void print_out_by(const MultiIndexContainer& s)
73{
74 /* obtain a reference to the index tagged by Tag */
75
76 const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& i=
77 get<Tag>(s);
78
79 typedef typename MultiIndexContainer::value_type value_type;
80
81 /* dump the elements of the index to cout */
82
83 std::copy(i.begin(),i.end(),std::ostream_iterator<value_type>(std::cout));
84}
85
86
87int main()
88{
89 employee_set es;
90
91 es.insert(employee(0,"Joe",31));
92 es.insert(employee(1,"Robert",27));
93 es.insert(employee(2,"John",40));
94
95 /* next insertion will fail, as there is an employee with
96 * the same ID
97 */
98
99 es.insert(employee(2,"Aristotle",2387));
100
101 es.insert(employee(3,"Albert",20));
102 es.insert(employee(4,"John",57));
103
104 /* list the employees sorted by ID, name and age */
105
106 std::cout<<"by ID"<<std::endl;
107 print_out_by<id>(es);
108 std::cout<<std::endl;
109
110 std::cout<<"by name"<<std::endl;
111 print_out_by<name>(es);
112 std::cout<<std::endl;
113
114 std::cout<<"by age"<<std::endl;
115 print_out_by<age>(es);
116 std::cout<<std::endl;
117
118 return 0;
119}