c++ - boost multi_index_container 损坏的索引

标签 c++ boost boost-multi-index

我有一个 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 是一个指向要更新的项目的迭代器
  • modfunctor接受单个参数(您要更改的对象)。

如果修改成功,则返回 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/

相关文章:

c++ - 通过索引快速搜索并保持 C++ 中的插入顺序

c++ - 在 C++11 中结合使用 std::function

c++ - 链接到静态 .lib 的问题链接到静态 .lib

c++ - boost::unordered_map 中的迭代器失效

c++ - 从值/引用映射分配给引用映射

c++ - 无法定义 multi_index_container ordered_non_unique

c++ - 如何释放 boost::multi_index::multi_index_container 使用的内存?

c++ - SystemParametersInfo 和 ERROR_OPERATION_IN_PROGRESS

c++ - C++ 中的结构是否类似于枚举或类?

c++ - 带有 const 指针的 boost::dynamic_pointer_cast 不起作用?