gpt4 book ai didi

c++ - boost multi_index_container 损坏的索引

转载 作者:搜寻专家 更新时间:2023-10-31 01:46:11 26 4
gpt4 key购买 nike

我有一个 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com