| 1 | /* Boost.MultiIndex example of member functions used as key extractors. | 
|---|
| 2 |  * | 
|---|
| 3 |  * Copyright 2003-2005 Joaquín M López Muñoz. | 
|---|
| 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/mem_fun.hpp> | 
|---|
| 18 | #include <boost/multi_index/ordered_index.hpp> | 
|---|
| 19 | #include <iostream> | 
|---|
| 20 | #include <string> | 
|---|
| 21 |  | 
|---|
| 22 | using namespace boost::multi_index; | 
|---|
| 23 |  | 
|---|
| 24 | /* A name record consists of the given name (e.g. "Charlie") | 
|---|
| 25 |  * and the family name (e.g. "Brown"). The full name, calculated | 
|---|
| 26 |  * by name_record::name() is laid out in the "phonebook order" | 
|---|
| 27 |  * family name + given_name. | 
|---|
| 28 |  */ | 
|---|
| 29 |  | 
|---|
| 30 | struct name_record | 
|---|
| 31 | { | 
|---|
| 32 |   name_record(std::string given_name,std::string family_name): | 
|---|
| 33 |     given_name(given_name),family_name(family_name) | 
|---|
| 34 |   {} | 
|---|
| 35 |  | 
|---|
| 36 |   std::string name()const | 
|---|
| 37 |   { | 
|---|
| 38 |     std::string str=family_name; | 
|---|
| 39 |     str+=" "; | 
|---|
| 40 |     str+=given_name; | 
|---|
| 41 |     return str; | 
|---|
| 42 |   } | 
|---|
| 43 |  | 
|---|
| 44 | private: | 
|---|
| 45 |   std::string given_name; | 
|---|
| 46 |   std::string family_name; | 
|---|
| 47 | }; | 
|---|
| 48 |  | 
|---|
| 49 | /* multi_index_container with only one index based on name_record::name(). | 
|---|
| 50 |  * See Compiler specifics: Use of const_mem_fun_explicit and | 
|---|
| 51 |  * mem_fun_explicit for info on BOOST_MULTI_INDEX_CONST_MEM_FUN. | 
|---|
| 52 |  */ | 
|---|
| 53 |  | 
|---|
| 54 | typedef multi_index_container< | 
|---|
| 55 |   name_record, | 
|---|
| 56 |   indexed_by< | 
|---|
| 57 |     ordered_unique< | 
|---|
| 58 |       BOOST_MULTI_INDEX_CONST_MEM_FUN(name_record,std::string,name) | 
|---|
| 59 |     > | 
|---|
| 60 |   > | 
|---|
| 61 | > name_record_set; | 
|---|
| 62 |  | 
|---|
| 63 | int main() | 
|---|
| 64 | { | 
|---|
| 65 |   name_record_set ns; | 
|---|
| 66 |  | 
|---|
| 67 |   ns.insert(name_record("Joe","Smith")); | 
|---|
| 68 |   ns.insert(name_record("Robert","Nightingale")); | 
|---|
| 69 |   ns.insert(name_record("Robert","Brown")); | 
|---|
| 70 |   ns.insert(name_record("Marc","Tuxedo")); | 
|---|
| 71 |  | 
|---|
| 72 |   /* list the names in ns in phonebook order */ | 
|---|
| 73 |  | 
|---|
| 74 |   for(name_record_set::iterator it=ns.begin();it!=ns.end();++it){ | 
|---|
| 75 |     std::cout<<it->name()<<std::endl; | 
|---|
| 76 |   } | 
|---|
| 77 |  | 
|---|
| 78 |   return 0; | 
|---|
| 79 | } | 
|---|