#include #include #include #include #include typedef int handle; typedef int group_id; struct NodeBase:boost::enable_shared_from_this { virtual ~NodeBase(){} virtual group_id memberOf()const=0; virtual handle getHandle()const=0; }; struct NodeType:NodeBase { NodeType(handle h,group_id gid):h(h),gid(gid){} virtual group_id memberOf()const{return gid;} virtual handle getHandle()const{return h;} private: group_id gid; handle h; }; struct NodeBaseHandleExtractor { typedef handle result_type; result_type operator()(const boost::shared_ptr& x) const { return x->getHandle(); } }; struct NodeBaseGroupIdExtractor { typedef group_id result_type; result_type operator()(const boost::shared_ptr& x) const { return x->memberOf(); } }; typedef boost::multi_index::multi_index_container< boost::shared_ptr, boost::multi_index::indexed_by< boost::multi_index::ordered_unique< NodeBaseHandleExtractor >, boost::multi_index::ordered_non_unique< NodeBaseGroupIdExtractor > > > NodePtrSet; int main() { NodePtrSet ns; typedef boost::shared_ptr element_type; ns.insert(element_type(new NodeType(0,10))); // 0 = id, 10 = group_id ns.insert(element_type(new NodeType(1,10))); ns.insert(element_type(new NodeType(2,20))); typedef boost::multi_index::nth_index::type index_t; const index_t& i=boost::multi_index::get<1>(ns); for(index_t::const_iterator it=i.begin();it!=i.end();){ std::cout<