【问题标题】:boost multi_index_container broken indexes提升 multi_index_container 损坏的索引
【发布时间】:2014-01-09 11:25:40
【问题描述】:

我有一个 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”方法对索引不做任何事情)。

【问题讨论】:

    标签: c++ boost boost-multi-index


    【解决方案1】:

    在对 Boost 多索引中的项目进行更改时,您应该使用索引对象公开的modify 方法。 modify 方法具有以下签名:

    bool modify(iterator position, Modifier mod);
    

    地点:

    • position 是一个迭代器,指向要更新的项目
    • mod 是一个 functor,它接受单个参数(您要更改的对象)。

    如果修改成功则返回true,如果修改失败则返回false

    modify函数运行时,functor更新你要修改的item,然后索引全部更新。

    例子:

    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

    【讨论】:

    • 谢谢...我会在几分钟内尝试:):):)
    • 成功了!经过一些修改。示例:“Chan::Ptr”而不是“Chan &chan”...再次感谢您!!! :):):):)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多