If Host contains a lot of data, you can use shared_ptr to avoid copying. You can use MultiIndex with shared_ptr in it:
#include <boost/multi_index_container.hpp> #include <boost/multi_index/member.hpp> #include <boost/multi_index/mem_fun.hpp> #include <boost/multi_index/hashed_index.hpp> #include <boost/shared_ptr.hpp> using namespace boost::multi_index; struct Host { int get_id() const { return id; } private: int id; // more members here }; typedef multi_index_container< boost::shared_ptr<Host>, // use pointer instead of real Host indexed_by< // hash using function Host::get_id hashed_unique< const_mem_fun<Host, int, &Host::get_id> > > // end indexed_by > HostContainer;
Then you can use it as follows:
int main() { HostContainer testHosts; Host * newHostPtr; newHostPtr = new Host; testHosts.insert( boost::shared_ptr<Host>(newHostPtr) ); return 0; }
Kirill V. Lyadvinsky
source share