Boost MultiIndex - objects or pointers (and how to use them?)?
Posted
by Sarah
on Stack Overflow
See other posts from Stack Overflow
or by Sarah
Published on 2010-04-15T19:02:41Z
Indexed on
2010/04/15
20:13 UTC
Read the original article
Hit count: 97
I'm programming an agent-based simulation and have decided that Boost's MultiIndex is probably the most efficient container for my agents. I'm not a professional programmer, and my background is very spotty. I've two questions:
- Is it better to have the container contain the agents (of class
Host
) themselves, or is it more efficient for the container to holdHost *
? Hosts will sometimes be deleted from memory (that's my plan, anyway... need to read up onnew
anddelete
). Hosts' private variables will get updated occasionally, which I hope to do through themodify
function in MultiIndex. There will be no other copies of Hosts in the simulation, i.e., they will not be used in any other containers. - If I use pointers to Hosts, how do I set up the key extraction properly? My code below doesn't compile.
// main.cpp - ATTEMPTED POINTER VERSION
...
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/tokenizer.hpp>
typedef multi_index_container<
Host *,
indexed_by<
// hash by Host::id
hashed_unique< BOOST_MULTI_INDEX_MEM_FUN(Host,int,Host::getID) > // arg errors here
> // end indexed_by
> HostContainer;
...
int main() {
...
HostContainer testHosts;
Host * newHostPtr;
newHostPtr = new Host( t, DOB, idCtr, 0, currentEvents );
testHosts.insert( newHostPtr );
...
}
I can't find a precisely analogous example in the Boost documentation, and my knowledge of C++ syntax is still very weak. The code does appear to work when I replace all the pointer references with the class objects themselves.
As best I can read it, the Boost documentation (see summary table at bottom) implies I should be able to use member functions with pointer elements.
© Stack Overflow or respective owner