简体   繁体   English

Boost MultiIndex - 对象或指针(以及如何使用它们?)?

[英]Boost MultiIndex - objects or pointers (and how to use them?)?

I'm programming an agent-based simulation and have decided that Boost's MultiIndex is probably the most efficient container for my agents. 我正在编写一个基于代理的模拟,并且已经确定Boost的MultiIndex可能是我的代理最有效的容器。 I'm not a professional programmer, and my background is very spotty. 我不是一个专业的程序员,我的背景很不稳定。 I've two questions: 我有两个问题:

  1. Is it better to have the container contain the agents (of class Host ) themselves, or is it more efficient for the container to hold Host * ? 让容器本身包含代理(类Host )是否更好,或者容器是否更有效地容纳Host * Hosts will sometimes be deleted from memory (that's my plan, anyway... need to read up on new and delete ). 主机有时会从内存中删除(这是我的计划,无论如何......需要阅读newdelete )。 Hosts' private variables will get updated occasionally, which I hope to do through the modify function in MultiIndex. 主机的私有变量偶尔会更新,我希望通过MultiIndex中的modify功能来实现。 There will be no other copies of Hosts in the simulation, ie, they will not be used in any other containers. 模拟中不会有其他主机副本,即它们不会在任何其他容器中使用。
  2. 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. 我在Boost文档中找不到一个精确类似的例子,而且我对C ++语法的了解仍然非常薄弱。 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. 最好我可以阅读它,Boost 文档 (见底部的摘要表)暗示我应该能够使用带有指针元素的成员函数。

If Host contains lot of data you could use shared_ptr to avoid copying. 如果Host包含大量数据,您可以使用shared_ptr来避免复制。 You could use MultiIndex with shared_ptr in it: 您可以在其中使用MultiIndex和shared_ptr

#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 could use it as follows: 然后你可以使用它如下:

int main()
{
   HostContainer testHosts;
   Host * newHostPtr;
   newHostPtr = new Host;
   testHosts.insert( boost::shared_ptr<Host>(newHostPtr) );

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM