【发布时间】:2010-04-15 19:02:41
【问题描述】:
我正在编写一个基于代理的模拟,并认为 Boost 的 MultiIndex 可能是我的代理最有效的容器。我不是专业的程序员,而且我的背景很参差不齐。我有两个问题:
- 让容器自己包含代理(
Host类)更好,还是让容器容纳Host *更有效?主机有时会从内存中删除(这是我的计划,无论如何……需要阅读new和delete)。主机的私有变量偶尔会更新,我希望通过 MultiIndex 中的modify函数来完成。模拟中不会有其他 Host 副本,即不会在任何其他容器中使用它们。 - 如果我使用指向主机的指针,如何正确设置密钥提取?我下面的代码无法编译。
// 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 );
...
}
我在 Boost 文档中找不到精确类似的示例,而且我对 C++ 语法的了解仍然很薄弱。当我用类对象本身替换所有指针引用时,代码似乎确实有效。
据我所知,Boost documentation(参见底部的汇总表)暗示我应该能够将成员函数与指针元素一起使用。
【问题讨论】: