2013-07-13 8 views
1

계산 집약적 인 프로그램을 작성하려고합니다. 그리고 multi_index_container의 composite_key_compare에 대한 comparsion의 필드가 char *가 필요합니다. 그러나, 그것은 작동하지 않는 것 같습니다. 아래 코드 :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이 요구되지 않는다는 것을 발견 두 번째로 "Mike"와 "Mike"를 비교합니다. 이걸 도와 줄 수있는 사람? composite_key_compare는 어떻게 작성해야합니까?

답변

0

나는 프로그램을 추적하여 boost가 "ordered unique"에 대한 비교 연산을 위해 std :: less()와 같은 것을 기대한다는 것을 알았다.

그래서 아래 코드를 추가하면 효과적입니다.

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; 

그러나 더 많은 질문이 있습니다.이 코드는 매우 길며, 더 많은 필드가 있으면 훨씬 더 길어질 것입니다. 이 작업을 수행하는 현명한 방법이 있습니까?