我有一个 multi_index 容器。 Chan::Ptr 是指向该对象的共享指针。 该容器有两个带有对象函数的索引。
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”方法对索引不执行任何操作)。
最佳答案
对 Boost 多重索引内的项目进行更改时,您应该使用 modify
索引对象公开的方法。 modify
方法具有以下签名:
bool modify(iterator position, Modifier mod);
地点:
position
是一个指向要更新的项目的迭代器mod
是 functor接受单个参数(您要更改的对象)。
如果修改成功,则返回 true
,如果失败,则返回 false
。
当修改函数运行时,仿函数会更新您要更改的项目,然后索引也会全部更新。
示例:
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"));
您可以找到更多信息here .
关于c++ - boost multi_index_container 损坏的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21018865/