2

我有一个 multi_index 容器。Chan::Ptr 是一个指向对象的 shared_pointer。该容器有两个带有对象函数的索引。

typedef multi_index_container<
    Chan::Ptr,
        indexed_by<
        ordered_unique<const_mem_fun<Chan,string,&Chan::Channel> >,         
        ordered_non_unique<const_mem_fun<Chan,string,&Chan::KulsoSzam> > >
    > ChanPtrLista;

直到我只将对象 push_back 到容器中,容器中的所有搜索都是成功的。

当我修改对象中的值(例如:Chan::Channel 更改)时,索引将被破坏。用索引列出容器,返回错误的顺序。但是,查找功能不再起作用。

如何重新索引容器?(“rearragne”方法对索引不做任何事情)。

4

1 回答 1

3

When making changes to an item inside a Boost multi index, you should use the modify method exposed by the index object. The modify method has the following signature:

bool modify(iterator position, Modifier mod);

Where:

  • position is an iterator pointing to the item to update
  • mod is a functor that accepts a single parameter (the object you want to change).

It returns true if the modification occurred successfully, or false if it failed.

When the modify function is run, the functor updates the item you want to change, and then the indexes are all updated.

Example:

class ChangeChannel
{
public:
  ChangeSomething(const std::string& newValue):m_newValue(newValue)
  {
  }

  void operator()(Chan &chan)
  {
    chan.Channel = m_newValue;
  }

private:
  std::string m_newValue;
};


typedef ChanPtrLista::index<0>::type ChannelIndex;
ChannelIndex& channelIndex = multiIndex.get<0>();

ChannelIndex::iterator it = channelIndex.find("Old Channel Value");

// Set the new channel value!
channelIndex.modify(it, ChangeChannel("New Channel Value"));

You can find more information here.

于 2014-01-09T11:40:54.893 回答