1

我正在尝试编写一个计算密集型程序。我需要 char* 作为 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,发现它确实在“Michael”与“Michael”的第一次比较时返回true,但我还发现equal_char没有被称为“Mike”与“Mike”的第二次比较。谁能帮我解决这个问题?我应该如何编写composite_key_compare?

4

1 回答 1

0

我跟踪了该程序,发现 boost 期望 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;

但我有更多的问题,这是非常长的代码,假设如果我有更多的字段,它会更长。有什么聪明的方法可以做到这一点吗?

于 2013-07-16T02:02:50.587 回答