1 /* Boost.MultiIndex example of functions used as key extractors.
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)
8 * See http://www.boost.org/libs/multi_index for library home page.
12 #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
13 #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
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>
23 using namespace boost::multi_index
;
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.
33 name_record(std::string given_name_
,std::string family_name_
):
34 given_name(given_name_
),family_name(family_name_
)
37 std::string
name()const
39 std::string str
=family_name
;
46 std::string given_name
;
47 std::string family_name
;
50 std::string::size_type
name_record_length(const name_record
& r
)
52 return r
.name().size();
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.
61 typedef multi_index_container
<
65 BOOST_MULTI_INDEX_CONST_MEM_FUN(name_record
,std::string
,name
)
68 global_fun
<const name_record
&,std::string::size_type
,name_record_length
>
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"));
82 /* list the names in ns in phonebook order */
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
;
90 /* list the names in ns according to their length*/
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
;