2013-07-13 31 views
1

我想編寫一個計算密集型程序。我需要char *作爲multi_index_container的composite_key_compare的比較字段。但是,它似乎並不奏效。代碼如下:boost multi_index_container composite_key_compare

struct MyStruct 
{ 
    char* firstName; 
    char* secondName; 
    int age; 
}; 

struct equal_char 
{ // functor for operator<= 
    inline bool operator()(const char* left, const char* right) const 
    { // apply operator<= to operands 
     bool result=(strcmp(left,right)==0); 
     return result; 
    } 
}; 

typedef composite_key 
    <MyStruct*, 
    BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, firstName), 
    BOOST_MULTI_INDEX_MEMBER(MyStruct, char*, secondName) 
    > comp_key; 
typedef multi_index_container 
    < 
    MyStruct*, 
    indexed_by 
     < 
     ordered_unique 
      < 
       comp_key, 
       composite_key_compare 
       <equal_char, equal_char> 
      > 
     > 
    > MyContainer; 

boost::ptr_vector<MyStruct> vec; 
MyStruct* struct1=new MyStruct(); 
struct1->firstName="Michael"; 
struct1->secondName="Mike"; 
struct1->age=20; 
vec.push_back(struct1); 



MyContainer myContainer; 
myContainer.insert(struct1); 
char* first="Michael"; 
char* second="Mike"; 
auto it=myContainer.find(boost::make_tuple(first, second)); 
if(it!=myContainer.end()) 
    cout << (*it)->age << endl; 

我沒有追查到equal_char,並發現它做了「邁克爾」到「邁克爾」的第一個比較返回true,但我也發現equal_char不叫的第二次比較「邁克」到「邁克」。任何人可以幫助我呢?我應該如何編寫composite_key_compare?

回答

0

我追溯了程序,發現boost需要像std :: less()這樣的「ordered unique」比較操作。

所以,我說下面的代碼,和它的工作。

struct CompareLess 
{ // functor for operator<= 
#pragma region For 1 variable 
    static inline int compare(const char* left, const char* right) 
    { 
     return strcmp(left, right); 
    } 
    inline bool operator()(const char* left, const char* right) const 
    { // apply operator<= to operands 
     return compare(left, right)<0; 
    } 

    static inline int compare(const boost::tuple<char*>& x, const char*y) 
    { 
     return compare(x.get<0>(),y); 
    } 
    inline bool operator()(const boost::tuple<char*>& x, const char*y) const 
    { 
     return compare(x,y)<0; 
    } 

    static inline int compare(const  boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*>& y) 
    { 
     return -compare(y,(const char*)(k.value->firstName)); 
    } 
    inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*>& y) const 
    { 
     return compare(k,y)<0; 
    } 

    static inline int compare(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) 
    { 
     return compare(y,(const char*)(k.value->firstName)); 
    } 
    inline bool operator()(const boost::tuple<char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) const 
    { 
     return compare(y,k) <0; 
    } 

#pragma endregion For 1 variable 
#pragma region For 2 variables 
    static inline int compare(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*, char*>& y) 
    { 
     int val=strcmp(k.value->firstName, y.get<0>()); 
     if(val!=0) 
      return val; 
     else 
      return compare(y.get<1>(),(const char*)(k.value->secondName)); 
    } 
    inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k, const boost::tuple<char*, char*>& y) const 
    { 
     return compare(k,y) <0; 
    } 

    static inline int compare(const boost::tuple<char*, char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) 
    { 
     return -compare(k,y); 
    } 
    inline bool operator()(const boost::tuple<char*, char*>& y, const boost::multi_index::composite_key_result<comp_key>& k) const 
    { 
     return compare(y,k)<0; 
    } 
#pragma endregion For 2 variables 
#pragma region For all variables 
    inline bool operator()(const boost::multi_index::composite_key_result<comp_key>& k1, 
     const boost::multi_index::composite_key_result<comp_key>& k2) const 
    { 
     return CompareLess()((const char*)(k1.value->firstName), (const char*)(k2.value->firstName)); 
    } 
#pragma endregion For all variables 



}; 

typedef multi_index_container 
    < 
    MyStruct*, 
    indexed_by 
     < 
     ordered_unique 
      < 
       comp_key, 
       CompareLess 
      > 
     > 
    > MyContainer; 

但我得到了更多的問題,這是非常冗長的代碼,並假設如果我有更多的領域,這將是非常非常多長。有沒有什麼聰明的方法來做到這一點?

相關問題