2014-01-09 33 views
2

我有一個multi_index容器。 Chan :: Ptr是該對象的一個​​shared_pointer。 容器有兩個具有對象功能的索引。boost multi_index_container折損索引

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」方法對索引不起作用)。

回答

3

當對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

+0

Thx ...我會在幾分鐘內嘗試:) :) :) –

+0

它的作品!經過一些修改。例如:「Chan :: Ptr」而不是「Chan&chan」... 再次謝謝你! :) :) :) :) –

+0

很高興我可以幫助:) –

相關問題