【问题标题】:boost multi_index_container composite_key_compareboost multi_index_container Composite_key_compare
【发布时间】:2013-07-13 01:25:34
【问题描述】:

我正在尝试编写一个计算密集型程序。而且我需要 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”与“麦克风”。谁能帮我解决这个问题?应该怎么写composite_key_compare?

【问题讨论】:

    标签: c++ boost containers multi-index boost-multi-index


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-06
      • 1970-01-01
      • 2014-12-02
      • 2010-12-14
      • 1970-01-01
      • 2021-04-25
      • 1970-01-01
      相关资源
      最近更新 更多