擦除和修改Boost MultiIndex容器中的元素

Sar*_*rah 7 c++ boost

我正在尝试在模拟中使用Boost MultiIndex容器.我对C++语法的了解非常薄弱,我担心我没有正确地从容器中删除元素或从内存中删除它.我还需要修改元素,我也希望在此确认语法和基本哲学.

// main.cpp
...
#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>
#include <boost/shared_ptr.hpp>
...
#include "Host.h" // class Host, all members private, using get fxns to access

using boost::multi_index_container;
using namespace boost::multi_index;

typedef multi_index_container<
  boost::shared_ptr< Host >,
  indexed_by< 
    hashed_unique< const_mem_fun<Host,int,&Host::getID> >
    //   ordered_non_unique< BOOST_MULTI_INDEX_MEM_FUN(Host,int,&Host::getAge) >
    > // end indexed_by
  > HostContainer;

typedef HostContainer::nth_index<0>::type HostsByID;

int main() {
   ...
   HostContainer allHosts;
   Host * newHostPtr;
   newHostPtr = new Host( t, DOB, idCtr, 0, currentEvents );
   allHosts.insert( boost::shared_ptr<Host>(newHostPtr) );
   // allHosts gets filled up

   int randomHostID = 4;
   int newAge = 50;
   modifyHost( randomHostID, allHosts, newAge );
   killHost( randomHostID, allHosts );
}

void killHost( int id, HostContainer & hmap ){
  HostsByID::iterator it = hmap.find( id );
  cout << "Found host id " << (*it)->getID() << "Attempting to kill. hmap.size() before is " << hmap.size() << " and ";
  hmap.erase( it ); // Is this really erasing (freeing from mem) the underlying Host object?
  cout << hmap.size() << " after." << endl;
}

void modifyHost( int id, HostContainer & hmap, int newAge ){
  HostsByID::iterator it = hmap.find( id );
  (*it) -> setAge( newAge ); // Not actually the "modify" function for MultiIndex...
}
Run Code Online (Sandbox Code Playgroud)

我的问题是

  1. allHostsshared_ptrs到Host对象的MultiIndex容器中,调用allHosts.erase( it )对象的shared_ptr的迭代器足以永久删除对象并从内存中释放它?它似乎是从容器中删除shared_ptr.
  2. allhosts容器目前有依赖于主机的ID一个功能指标.如果我引入一个调用成员函数(Host :: getAge())的有序第二个索引,其中年龄在模拟过程中发生变化,当我引用它时索引总是会更新吗?
  3. 使用MultiIndex的修改来修改底层对象的年龄与我上面显示的方法有什么区别?
  4. 我对MultiIndex中假定/需要保持不变的内容感到困惑.

提前致谢.


更新

modify根据我在相关的Boost 示例中看到的内容,这是我尝试获得语法的尝试.

struct update_age {
  update_age():(){} // have no idea what this really does... elicits error
  void operator() (boost::shared_ptr<Host> ptr) {
    ptr->incrementAge(); // incrementAge() is a member function of class Host
  }
};
Run Code Online (Sandbox Code Playgroud)

然后modifyHost,我有hmap.modify(it,update_age).即使通过一些奇迹证明这是正确的,我也喜欢对正在发生的事情做出某种解释.

Kir*_*sky 8

shared_ptr将删除Host其析构函数中的实际对象(如果没有其他实例shared_ptr).MultiIndex中的所有对象都被视为常量.要修改对象,您应该使用modifyMultiIndex 方法.在这种情况下,索引将在必要时进行更新.

您可以使用以下仿函数来更改age字段:

  struct change_age
  {
    change_age(int age) : age_(age) {}    
    void operator()(boost::shared_ptr<Host> h) // shared_ptr !!!
    {
      h->age = age_;
    }

  private:
    int age_;
  };
Run Code Online (Sandbox Code Playgroud)

然后使用如下:

  testHosts.modify( it, Host::change_age( 22 ) ); // set age to 22
Run Code Online (Sandbox Code Playgroud)