2010-06-10 70 views
1

我在搜索引擎的搜索結果中搜索了一下,但沒有找到任何示例。無論如何,這可能是一個愚蠢的問題。如何使用composite_key爲multi_index_containder編寫自定義謂詞?

因此,我們必須從男人著名的電話簿:)

typedef multi_index_container< 
    phonebook_entry, 
    indexed_by< 
    ordered_non_unique< 
     composite_key< 
     phonebook_entry, 
     member<phonebook_entry,std::string,&phonebook_entry::family_name>, 
     member<phonebook_entry,std::string,&phonebook_entry::given_name> 
     >, 
     composite_key_compare< 
     std::less<std::string>, // family names sorted as by default 
     std::greater<std::string> // given names reversed 
     > 
    >, 
    ordered_unique< 
     member<phonebook_entry,std::string,&phonebook_entry::phone_number> 
    > 
    > 
> phonebook; 


phonebook pb; 
... 
// look for all Whites 
std::pair<phonebook::iterator,phonebook::iterator> p= 
    pb.equal_range(boost::make_tuple("White"), my_custom_comp()); 

應該如何my_custom_comp(是什麼樣子?我的意思是對我來說很明顯,然後它需要boost::multi_index::composite_key_result<CompositeKey>作爲一個論據(由於編譯錯誤:)),但是在這種特殊情況下CompositeKey是什麼?

struct my_custom_comp 
{ 
    bool operator()(?? boost::multi_index::composite_key_result<CompositeKey> ??) const 
    { 
     return blah_blah_blah; 
    } 
}; 

在此先感謝。

回答

2

它應該看起來像composite_key_compare。對於你的情況(非模板版本):

typedef composite_key< 
    phonebook_entry, 
    member<phonebook_entry,std::string,&phonebook_entry::family_name>, 
    member<phonebook_entry,std::string,&phonebook_entry::given_name> 
    > my_comp_type_t; 

struct my_custom_comp 
{ 
    bool operator()( 
     const boost::tuple<const char*>& x, 
     const boost::multi_index::composite_key_result<my_comp_type_t>& y) const 
    { 
     return false; // should return something instead of false 
    } 
    bool operator()( 
     const boost::multi_index::composite_key_result<my_comp_type_t>& y, 
     const boost::tuple<const char*>& x) const 
    { 
     return false; // should return something instead of false 
    } 
}; 
+0

請你指出在上面的例子中什麼是CompositeKey類型(在這裏使用 - composite_key_result )? 我確實很感謝答案。 – Titan 2010-06-10 12:46:21

+0

'CompositeKey'是一個模板參數。檢查我的答案中的鏈接。 – 2010-06-10 13:14:48

+0

非常感謝你:) – Titan 2010-06-11 08:20:37