/* Boost.MultiIndex basic example. * * Copyright 2003-2005 Joaquín M López Muñoz. * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * See http://www.boost.org/libs/multi_index for library home page. */ #if !defined(NDEBUG) #define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING #define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE #endif #include #include #include #include #include #include #include using boost::multi_index_container; using namespace boost::multi_index; /* an employee record holds its ID, name and age */ struct employee { int id; std::string name; int age; employee(int id_,std::string name_,int age_):id(id_),name(name_),age(age_){} friend std::ostream& operator<<(std::ostream& os,const employee& e) { os<, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>, ordered_non_unique< tag,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>, ordered_non_unique< tag, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> > > employee_set; template void print_out_by( const MultiIndexContainer& s, Tag* =0 /* fixes a MSVC++ 6.0 bug with implicit template function parms */ ) { /* obtain a reference to the index tagged by Tag */ const typename boost::multi_index::index::type& i= get(s); typedef typename MultiIndexContainer::value_type value_type; /* dump the elements of the index to cout */ std::copy(i.begin(),i.end(),std::ostream_iterator(std::cout)); } class XXX { employee_set empls; }; int main() { XXX xxx; XXX yyy; yyy=xxx; employee_set es; es.insert(employee(0,"Joe",31)); es.insert(employee(1,"Robert",27)); es.insert(employee(2,"John",40)); /* next insertion will fail, as there is an employee with * the same ID */ es.insert(employee(2,"Aristotle",2387)); es.insert(employee(3,"Albert",20)); es.insert(employee(4,"John",57)); /* list the employees sorted by ID, name and age */ std::cout<<"by ID"<(es); std::cout<(es); std::cout<(es); std::cout<