basic.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /* 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. #if !defined(NDEBUG)
  11. #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
  12. #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
  13. #endif
  14. #include <boost/multi_index_container.hpp>
  15. #include <boost/multi_index/member.hpp>
  16. #include <boost/multi_index/ordered_index.hpp>
  17. #include <algorithm>
  18. #include <iostream>
  19. #include <iterator>
  20. #include <string>
  21. using boost::multi_index_container;
  22. using namespace boost::multi_index;
  23. /* an employee record holds its ID, name and age */
  24. struct employee
  25. {
  26. int id;
  27. std::string name;
  28. int age;
  29. employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){}
  30. friend std::ostream& operator<<(std::ostream& os,const employee& e)
  31. {
  32. os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
  33. return os;
  34. }
  35. };
  36. /* tags for accessing the corresponding indices of employee_set */
  37. struct id{};
  38. struct name{};
  39. struct age{};
  40. /* see Compiler specifics: Use of member_offset for info on
  41. * BOOST_MULTI_INDEX_MEMBER
  42. */
  43. /* Define a multi_index_container of employees with following indices:
  44. * - a unique index sorted by employee::int,
  45. * - a non-unique index sorted by employee::name,
  46. * - a non-unique index sorted by employee::age.
  47. */
  48. typedef multi_index_container<
  49. employee,
  50. indexed_by<
  51. ordered_unique<
  52. tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
  53. ordered_non_unique<
  54. tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
  55. ordered_non_unique<
  56. tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
  57. > employee_set;
  58. template<typename Tag,typename MultiIndexContainer>
  59. void print_out_by(const MultiIndexContainer& s)
  60. {
  61. /* obtain a reference to the index tagged by Tag */
  62. const typename boost::multi_index::index<MultiIndexContainer,Tag>::type& i=
  63. get<Tag>(s);
  64. typedef typename MultiIndexContainer::value_type value_type;
  65. /* dump the elements of the index to cout */
  66. std::copy(i.begin(),i.end(),std::ostream_iterator<value_type>(std::cout));
  67. }
  68. int main()
  69. {
  70. employee_set es;
  71. es.insert(employee(0,"Joe",31));
  72. es.insert(employee(1,"Robert",27));
  73. es.insert(employee(2,"John",40));
  74. /* next insertion will fail, as there is an employee with
  75. * the same ID
  76. */
  77. es.insert(employee(2,"Aristotle",2387));
  78. es.insert(employee(3,"Albert",20));
  79. es.insert(employee(4,"John",57));
  80. /* list the employees sorted by ID, name and age */
  81. std::cout<<"by ID"<<std::endl;
  82. print_out_by<id>(es);
  83. std::cout<<std::endl;
  84. std::cout<<"by name"<<std::endl;
  85. print_out_by<name>(es);
  86. std::cout<<std::endl;
  87. std::cout<<"by age"<<std::endl;
  88. print_out_by<age>(es);
  89. std::cout<<std::endl;
  90. return 0;
  91. }