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