fun_key.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* Boost.MultiIndex example of functions used as key extractors.
  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. #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/global_fun.hpp>
  16. #include <boost/multi_index/mem_fun.hpp>
  17. #include <boost/multi_index/ordered_index.hpp>
  18. #include <iostream>
  19. #include <string>
  20. using namespace boost::multi_index;
  21. /* A name record consists of the given name (e.g. "Charlie")
  22. * and the family name (e.g. "Brown"). The full name, calculated
  23. * by name_record::name() is laid out in the "phonebook order"
  24. * family name + given_name.
  25. */
  26. struct name_record
  27. {
  28. name_record(std::string given_name_,std::string family_name_):
  29. given_name(given_name_),family_name(family_name_)
  30. {}
  31. std::string name()const
  32. {
  33. std::string str=family_name;
  34. str+=" ";
  35. str+=given_name;
  36. return str;
  37. }
  38. private:
  39. std::string given_name;
  40. std::string family_name;
  41. };
  42. std::string::size_type name_record_length(const name_record& r)
  43. {
  44. return r.name().size();
  45. }
  46. /* multi_index_container with indices based on name_record::name()
  47. * and name_record_length().
  48. * See Compiler specifics: Use of const_mem_fun_explicit and
  49. * mem_fun_explicit for info on BOOST_MULTI_INDEX_CONST_MEM_FUN.
  50. */
  51. typedef multi_index_container<
  52. name_record,
  53. indexed_by<
  54. ordered_unique<
  55. BOOST_MULTI_INDEX_CONST_MEM_FUN(name_record,std::string,name)
  56. >,
  57. ordered_non_unique<
  58. global_fun<const name_record&,std::string::size_type,name_record_length>
  59. >
  60. >
  61. > name_record_set;
  62. int main()
  63. {
  64. name_record_set ns;
  65. ns.insert(name_record("Joe","Smith"));
  66. ns.insert(name_record("Robert","Nightingale"));
  67. ns.insert(name_record("Robert","Brown"));
  68. ns.insert(name_record("Marc","Tuxedo"));
  69. /* list the names in ns in phonebook order */
  70. std::cout<<"Phonenook order\n"
  71. <<"---------------"<<std::endl;
  72. for(name_record_set::iterator it=ns.begin();it!=ns.end();++it){
  73. std::cout<<it->name()<<std::endl;
  74. }
  75. /* list the names in ns according to their length*/
  76. std::cout<<"\nLength order\n"
  77. << "------------"<<std::endl;
  78. for(nth_index<name_record_set,1>::type::iterator it1=get<1>(ns).begin();
  79. it1!=get<1>(ns).end();++it1){
  80. std::cout<<it1->name()<<std::endl;
  81. }
  82. return 0;
  83. }