~ubuntu-branches/debian/sid/boost1.49/sid

« back to all changes in this revision

Viewing changes to libs/multi_index/example/basic.cpp

  • Committer: Package Import Robot
  • Author(s): Steve M. Robbins
  • Date: 2012-02-26 00:31:44 UTC
  • Revision ID: package-import@ubuntu.com-20120226003144-eaytp12cbf6ubpms
Tags: upstream-1.49.0
ImportĀ upstreamĀ versionĀ 1.49.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Boost.MultiIndex basic example.
 
2
 *
 
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)
 
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
 
 
24
using boost::multi_index_container;
 
25
using namespace boost::multi_index;
 
26
 
 
27
/* an employee record holds its ID, name and age */
 
28
 
 
29
struct 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
 
 
46
struct id{};
 
47
struct name{};
 
48
struct 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
 
 
60
typedef 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
 
 
71
template<typename Tag,typename MultiIndexContainer>
 
72
void print_out_by(
 
73
 const MultiIndexContainer& s,
 
74
 Tag* =0 /* fixes a MSVC++ 6.0 bug with implicit template function parms */
 
75
)
 
76
{
 
77
  /* obtain a reference to the index tagged by Tag */
 
78
 
 
79
  const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& i=
 
80
    get<Tag>(s);
 
81
 
 
82
  typedef typename MultiIndexContainer::value_type value_type;
 
83
 
 
84
  /* dump the elements of the index to cout */
 
85
 
 
86
  std::copy(i.begin(),i.end(),std::ostream_iterator<value_type>(std::cout));
 
87
}
 
88
 
 
89
 
 
90
int main()
 
91
{
 
92
  employee_set es;
 
93
 
 
94
  es.insert(employee(0,"Joe",31));
 
95
  es.insert(employee(1,"Robert",27));
 
96
  es.insert(employee(2,"John",40));
 
97
 
 
98
  /* next insertion will fail, as there is an employee with
 
99
   * the same ID
 
100
   */
 
101
 
 
102
  es.insert(employee(2,"Aristotle",2387));
 
103
 
 
104
  es.insert(employee(3,"Albert",20));
 
105
  es.insert(employee(4,"John",57));
 
106
 
 
107
  /* list the employees sorted by ID, name and age */
 
108
 
 
109
  std::cout<<"by ID"<<std::endl;
 
110
  print_out_by<id>(es);
 
111
  std::cout<<std::endl;
 
112
 
 
113
  std::cout<<"by name"<<std::endl;
 
114
  print_out_by<name>(es);
 
115
  std::cout<<std::endl;
 
116
 
 
117
  std::cout<<"by age"<<std::endl;
 
118
  print_out_by<age>(es);
 
119
  std::cout<<std::endl;
 
120
 
 
121
  return 0;
 
122
}